Fast New PSCustomObject.
June 15th, 2008 by KarlGiven the context on the last few posts. I’ve made a simple helper method in C# that can take a simple powershell hashtable and create a PSCustomObject based on it. Here is an example of how you can call it.
[snapinini.newobjecthelper]::newObjectFast(@{name="karl";age=31;now = [datetime]::Now})
very simple and its at least 6 times faster than the closest other technique in powershell. Most of hte overhead is in creating the hashtable. (otherwise its 90 times faster). The hashtable syntax is very convenient however maybe even its overhead is too much and we deserve a better way.
Here is the C# method. Its pretty basic stuff.
public static PSObject newObjectFast( Hashtable noteproperties )
{
PSObject obj = new PSObject();
if (noteproperties != null)
foreach(DictionaryEntry item in noteproperties)
obj.Properties.Add(new PSNoteProperty((string)item.Key,item.Value));
return obj;
}
- Karl
Posted in Bare Metal, Gotchas, Nuances and Facets, Powershell, performance |

Entire RSS
June 15th, 2008 at 10:14 am
[…] Fast New PSCustomObject. […]
June 16th, 2008 at 7:00 am
Karl,
Can you explain this part in more detail? “its at least 6 times faster than the closest other technique in powershell”?
What are you comparing it to, what times did you see?
June 18th, 2008 at 10:22 am
Hal, 6 times faster than select-object method, or about 30 times faster than using add-member