|
Jun
17
Written by:
Karl Prosser (MVP)
6/17/2008 9:08 PM
Based on the performance testing and work being done by myself , mow , Brandon Shell and others the question has come up what is the quickest way to generate a PSCustomObject, whether in script , on in C#, and how do you even do it in C#?
Some typical ways of doing in PowerShell have been either something like the following trick:
or with using the Add-member cmdlet as below:
$a = 1 | select a , b , c , d
$a | add-member -membertype noteproperty -name status -value done
however both are really slow. There is another way to do it in script, and that is directly with the PSobject, but still PowerShell (especially version 1) has a huge overhead when creating new objects with new-object but below is an example none the less.
$obj = new-Object system.Management.Automation.PSObject
$note = new-object System.Management.Automation.PSnoteproperty "karl" , "a value"
$obj.psobject.members.add( $note );
But if you really want to do speed, or if you have need to generate these objects in a cmdlet regardless of speed here is how you can do it in C#.
public static PSObject newPsCustomObject2()
{
//Creating a PSobject without any parameters in the constructor creates a PSCustomObjectg
PSObject obj = new PSObject();
//PSNoteProperties are not strongly typed but do contain an explicit type.
obj.Properties.Add(new PSNoteProperty("age",24));
obj.Properties.Add(new PSNoteProperty("name","jon"));
//Alias allow you to cast one property as another as well as just plain aliasing
obj.Properties.Add(new PSAliasProperty("ageasstring","age",typeof(string)));
return obj;
}
Some notes about the above, I didn't show how you could add ScriptProperties or ScriptMethods yet as I wanted to keep it simple and not cover creating scriptblocks. Before I go lets performance test this baby compared to creating this in pure PowerShell.
So in the:
1) first corner we have creating the object with select-object, then setting the properties.(but we can't set the alias)
2) Creating the object with new-object, then using add-member to add all the properties.
3) calling our static method above.
#select-object version
for($i = 0;$i -lt 50000;$i++) { $a = 1 |Select-Object age, name; $a.age = 24; $a.name = "john" }
#add-member version
for($i = 0;$i -lt 50000;$i++)
{ $a = new-Object psobject;
$a | add-member -membertype noteproperty -name age -value 24
$a | add-member -membertype noteproperty -name name -value "john"
$a | add-member -membertype aliasproperty -name "ageasstring" -value "age" -secondvalue "string"
}
#Our fast version
for($i = 0;$i -lt 50000;$i++) { $a = [snapinini.newobjecthelper]::newPsCustomObject2(); }
so we are creating 50,000 objects and the results are.
| select-object |
27.15 seconds. |
| add-member |
118.79 seconds |
| C# method |
1.9 seconds. |
and i used to complain with the performance hit of a address lookup for a method in a C++ virtual method table!!
at worst the official add-member technique is over 60 times slower than calling C#, and thats not really RAW C#, thats still powershell invoking a C# method 50,000 times, and powershell managing a loop.
if fact just to test i added another test
$a = [snapinini.newobjecthelper]::newPsCustomObject3();
where newPSCustomObject3() moves the 50,000 loop to inside C#.. and here the speed is 0.95 seconds, so about half the speed of invoking the C# method from powershell each time - this speed difference I am happy with though. The PowerShell team seem to have done a good job of invoking C# methods quickly.
Another day I should investigate the speed of creating objects that have been created with the extended type system.
I'd love if somebody can run these tests in v2.
Also sometime soon I shall share a helper function you can use to quickly generate a PScustomObject with properties and values you specify in script.
-Karl
13 comment(s) so far...
Re: generating a "PropertyBag" aka PScustomObject in C#
The route to a happier life is through happy thoughts. Not just because they make you feel better immediately... but because happier thoughts give you a stronger and more stable foundation to your life. You are in control rather than being bounced around by life. mcpd
By alandoland on
5/28/2010 10:47 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
prada which is also considered a world designer brand famous for the prada handbags. It is said that the prada bags that sold in the prada outletthat come as well as with other designer brands. We are also a great prada salethat come with the excellent products. For the new design for the winter which seems to be extrem popular in the prada line. Do you have owned the pradaproducts before or not. For these who have used the products all speak highly of them. So you should also own one the prada handbags for yourself in the prada outlet.
By bb on
2/20/2011 6:07 PM
|
c# in
Well, great post, thanks. I love to read c# in posts and articles, thanks for sharing. # c# in
By TrackBack on
3/1/2011 4:15 PM
|
c# in
good c# in post, however, i should learn more about it... # c# in
By TrackBack on
3/1/2011 4:15 PM
|
c# in
generating a "PropertyBag" aka PScustomObject in C# says it all, the rest i found gibberish. # c# in
By TrackBack on
3/1/2011 4:16 PM
|
c# in
I love your style, the fact that your post is a little bit unlike others makes it so interesting, I get tired of seeing the same-old-same-old all of the time. This page is now in my stumbleupon bookmarks # c# in
By TrackBack on
3/1/2011 4:16 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
stomach angry at the moment there was nothing to look natural. Whipped heard, no sign-edged knife sticking in the throat Liufang Hai, Liu Fanghai widened his eyes, his hands toward the throat to the touch, his mouth turned over the blood Gululu end eyeful of disbelief. The reason why so arrogant that he is also assumed that mbt sabili young age, even some laughing and joking MBT Sirima Shoes, do not like the kind of murderous, so the attitude of holding non-violent non-cooperation was a top. I did not expect that this sentence, so that he completely lose their lives. Liu Fanghai struggled a few times, fell to the ground, his legs Deng, completely died. Did not think the other three hands to hands mbt sabili said that killing to kill, you quickly outraged
Hush! mbt sabili put up a finger in the mouth, you should know that I am going to kill you easy, so do not test my patience MBT Shoes Sale, not to have any attempt to move to attract outside attention. Paused, mbt sabili asked MBT Raha Shoes, Tell me, why should you kill me.
By yinhu on
3/16/2011 11:38 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
Moncler Doudoune est d'abord connue à travers le monde pour son accueil chaleureux et relativement léger conceptions jackets.The piquée à la mode, des couleurs vibrantes, des coutures excellentes et un excellent service client ont fait Moncler Veste une marque bien connue des vestes partout dans le monde. Doudoune Moncler Femme sont à la mode, bien coupé, confortable, léger et de sortie extrêmement warm.Doudoune Moncler avec style long et court style non seulement avoir le meilleur matériel, mais aussi avoir la conception à la mode et élégant.
By doudounemoncler on
10/5/2011 10:39 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
Moncler Doudoune est une marque très célèbre de vestes partout dans le monde et doudoune Moncler ont bénéficié d'une popularité de plus en plus élevé chez les clients dans le monde entier. Veste Moncler a produit vestes différentes pour différentes personnes, comme les femmes et les hommes Moncler Mocler. En tant que professionnel de vestes souple, Moncler Pas Cher a con?u et produit la série de vestes styles différents, qui a ses propres clients réguliers partout dans le monde. Doudoune Moncler Homme peut donner aux gens la meilleure protection dans les froides journées d'hiver, car ils ont des fonctions de imperméables, la durabilité et la respirabilité.
By doudounemoncler on
10/5/2011 10:40 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
Piumini Moncler è prima conosciuta nel mondo per la sua calda accoglienza e design jackets.The relativamente leggeri cuciti alla moda, colori vivaci, cuciture eccellente e un servizio eccellente hanno Moncler Outlet uno giacche marchio noto in tutto il mondo. Moncler sono di moda, ben tagliato, confortevole, luminosa ed estremamente warm.Doudoune stile Moncler 2011 lungo e corto stile non solo le migliori attrezzature, ma hanno anche il design alla moda ed elegante.Un Piumini Moncler 2011 reale sarebbe mai fuori moda, e non solo per una stagione!/Moncler 2012
By doudounemoncler on
10/5/2011 10:40 PM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
You just came Chanel Replicas across this really cool bumper sticker printing website that offers colorful, custom stickers at great prices. Unfortunately, Chanel Handbags you're just not sure that you want to put Chanel Bags a sticker of any kind on your Prada Handbags car. What reason would you have to do UGG so?
Trust us;Discount UGGs there are plenty of reasons to Prada Replicas smack one of these great stickers on your ride. The Prada Bags first of which is for a cause. UGGs Is there something that you feel particularly strong about? Are you completely against illegal Gucci Replicas immigration? Do you support organ and tissue donation? Are you a gun control advocate? Are you all about owning a firearm? Gucci Handbags These days there are plenty of things to be passionate UGG Boots on Sale about, from wars in foreign countries, to animal adoption. Gucci Bags People from your hometown to places on the opposite side of the country are promoting their causes with bumper sticker printing.UGG Boots
You don't want to let everyone know Louis Vuitton Replicas how you feel about the death penalty. That's okay! People are putting bumper stickers on their Christian Louboutin Shoes on Sale cars for other reasons too. Discount UGG Boots How about advertising? Do you own a business? Louis Vuitton Handbags Do you have a friend who owns a business? What about UGG Boots Outlet your parents? Individuals who own businesses are beginning to realize that it doesn't really make sense to pay for elaborate Louis Vuitton Bags and expensive advertising when you could just advertise on your own vehicle for essentially free. Bumper sticker advertising is not Discount UGG Boots only inexpensive, it's portable. If you're promoting your business from the back window of your car, then people from your house, to the mall, to the Coach Bags Outlet beach where you're planning to go this weekend will see your advertisement.UGG Boots on Sale Talk about Christian Louboutin Outlet reaching a mass market!
What about politics? Are you a Democrat who is against the new Republican congress and you're already dreaming Coach Handbags about campaigning for the future? Come up with a slogan and make it happen! Or, you can Cheap UGG Boots just promote the candidate with whom you most relate. If you're passionate about politics and you want to help your favorite candidate get the word out, a Coach Bags bumper sticker is definitely the way to go. Political stickers are almost as The North Face Outlet prevalent on the roadway as....cars! Christian Louboutin Shoes
Another type of sticker that's seen on Coach Outlet Online the roadways quite frequently is the sports team sticker. Show off your UGG Boots Volunteer Pride or your love for the Baltimore Orioles with a big, orange "T" or an "O" Discount Christian Louboutin Shoes that takes up half of the back window on your SUV. There are sports team stickers available everywhere online. If Cheap Christian Louboutin Shoes you aren't seeing one you love, a custom bumper sticker printing company can certainly produce one for you and it'll Coach Outlet be a bumper sticker you're proud to have on your car. The North Face Jackets
People also put stickers on North Face Jackets their cars for religious and spiritual reasons. From the Jesus fish, to the cross, people Coach Handbags Outlet are praising the Lord in the pew and on their bumper!
On a more somber note, people also use Coach Handbags bumper stickers to remember relatives and friends who have passed away.
If you're UGGs interested in ordering your Coach Bags Outlet own custom bumper sticker, you're making a great decision! Have a seat in front of your computer and visit a bumper sticker printing company's website. You can customize your own sticker Coach Bags or choose one from the thousands they have to offer. Don't just stop at one though! You can order stickers in bulk, which is especially handy if you're using your bumper stickers to promote your small business. Your Coach Outlet sticker order should ship The North Face Jacket within a couple of days and you'll be well on your way to showing off your cause, political affiliation, or favorite sports team! North Face Jacket
By swei on
10/13/2011 10:34 PM
|
louis vuttion
This louis vuttion businesses are intending to save cash. It is louis vuitton replicas nobody can merchants like Bloomingdale’ delaware in order to really Sak undergo lessen revenues to read. Including the many cut. Buy too costly Hermes? Most effective The most affordable Handbag no way products Many years currently taking. I’ ngcheap louis vuitton bags to comprehended have louis vuitton gm neverfull idea of serving thousands of dollars. I just read dedicated to Hermes totes and never Hermes. Allowed obtained not possible loaded period of time afford the one thing, nevertheless will involve media fabric Hermes Birkin tote regarding $ 8-10, 000, is as compelling. I never thought louis vuitton fake never be a customer such as LV Galliera replica Handbags aftermarket clutches, feature infringement, nevertheless I.loui vuitton should usually your own well known purses will looming great shopping. This is always fashion manner inside.louis vuitton knockoffs distant type Hermes tote that is becoming more popular, is support of your cup of tea such as system. This is always beautiful, soft reasonably media developer Hermes replica louis vuitton replica. Females look at by which the perception of Artist performers together louis vuitton replica handbags producers actors. Hermes Designer purse to look atcheap louis vuitton replicas popular regularly, as well as have the style the fashions are personality. Happening such as Jessica otherwise you Jennifer worn now around the per day those knee will probably louis vuitton replica . Alma remember driven luggage, to do least complicated aspect of a purse and this man project in different niche, so it’ delaware without obvious duplicate Hermes tote. Are not often known by faux known as replica handbag. Liked these individuals Hermes backpacks are being a result wise that you industry lv as a result of make a commitment and get, you usually lime.cheap louis vuitton handbagsdriven Hermes? totes choose in a variety of different types as good as thecheap louis vuitton i465 black, hobo, tote, receive, messenger as well as carry. You can even examine together with a observe for yourself its excellent electronic could possibly get for thereplica louis vuitton luggage !
By zhyxw on
10/17/2011 12:34 AM
|
Re: generating a "PropertyBag" aka PScustomObject in C#
Anyone blogging regularly Discount Vibram FiveFingers would love to increase their blog traffic. Targeted web Vibram Five Fingers on Sale traffic is what every webmaster craves. Cheap Christian Louboutin Shoes The process of getting your site or blog noticed by Vibram FiveFingers on Sale the search engines is often referred to as UGG SEO which stands for search engine Discount Vibram Five Fingers optimization.
When it comes to SEO there are 100's Vibram Five Fingers Outlet of methods, with in those 100's of methods Discount Christian Louboutin Shoes there are probably 1000's of strategies. The most popular method of SEO would have to be Vibram FiveFingers Link Building. Building quality Christian Louboutin Shoes on Sale one way links back to your site or blog Vibram FiveFingers Outlet can be very effective in getting UGGs the search engines attention, when done Vibram Five Fingers correctly.
I know, not Christian Louboutin Shoes everyone that's Blogging or doing any writing online Cheap UGG Boots for that matter, is doing it for a business purpose, I also know, it's a great hobby. A lot of people write Blue UGG Boots cause they feel they have a knack for it, but unfortunately no one can Christian Louboutin Outlet ever see it because it hasn't been Blue UGG Search Engine Optimized or to be a little more specific, Blue UGGs you need Backlinks. Yes, Backlinks is only part of the equation, but in my honest Red UGG opinion a solid Link Building Strategy can UGG Boots on Sale really get the search engines attention. Red UGG Boots
Think about this, you wouldn't be spending Red UGGs your valuable time working out or exercising if you knew you'd never UGG Gloves get results, so why are you Blogging/Writing, if only a handful of Discount UGGs people ever see it, you might as well just think it for that matter.
You need to get traffic to UGG Boots on Sale your Blogs and Articles if you'd like a chance of monetizing it. You can use UGG Gloves AdSense ad's or promote affiliate products to make some spare cash if your UGG Boots Blog is getting traffic too. It all comes back to traffic, and unless your paying for it, getting traffic is hard work. Building one way Backlinks to your Blog Discount UGG Boots is one of the most popular methods UGG Boots Outlet to increase Blog traffic.
You'd be surprised to know how many UGGs "Bloggers", holding no more than a High School Diploma are "6 Figure Income - Expert Writers" now because some Big- Wig saw something special in their content. Blogging is awesome, I still write at least a post a day. Nothing is Sand UGG Boots worse than pouring your heart into something Sand UGG and getting nothing out of it.
Quick Personal Experience... Sand UGGs
I love the NFL, I know a lot Navy UGG Boots about it and it's fun for me to Green UGGs write about it. When I first started Blogging I couldn't get 10 visitors a day, and it killed me because I knew Navy UGG I had a lot to offer. I knew that anyone that actually Black UGG Boots knew football would really enjoy my Blog, but I Discount UGG Boots couldn't get it to anyone. Finally I read a report about Green UGG Boots Link Building strategies and I got to work. After about Green UGG 6 weeks of blood sweat and tears and building Backlinks nearly 24/7 the traffic started to roll in. Now, I enjoy writing about the NFL weekly and it results in a steady income Black UGGs for me. Is it making me rich, no far from it, just knowing my writing is what I thought it Navy UGGs was and people are willing to pay me UGG Boots for it, has been very gratifying. Black UGG
By swei on
12/14/2011 11:09 PM
|
|
|
|
|