An open API service indexing awesome lists of open source software.

https://github.com/deadlydog/PowerShell.Experiment.ClassPerformanceComparison

A test repo to compare the performance of the different ways to define classes in PowerShell
https://github.com/deadlydog/PowerShell.Experiment.ClassPerformanceComparison

Last synced: 2 months ago
JSON representation

A test repo to compare the performance of the different ways to define classes in PowerShell

Awesome Lists containing this project

README

          

# PowerShell class performance comparison test

This repo is used to run tests to compare the performance of the different ways to define and use classes in PowerShell:

- PowerShell classes
- Inline C# classes
- Compiled C# classes (import an assembly)

You can read more about this experiment and the results [in this blog post](https://blog.danskingdom.com/PowerShell-class-definition-pros-cons-and-performance-comparison/).

## The test

The same classes and enums are defined in each of the three ways, and are imported multiple times into a PowerShell session.
The test measures how long it takes to import the class and enum types into the script.

To run the test, run the [Invoke-Test.ps1](/src/Invoke-Test.ps1) script.

### Prerequisites

The test requires dotnet.exe to be installed and available in the PATH, as it is used to build the C# assemblies imported by the test.

## Results

The results of the test on my local machine are:

```text
ImportPowerShellClasses1: 89ms
ImportPowerShellClasses2: 10ms
ImportPowerShellClasses3: 9ms
ImportPowerShellClassesLarge1: 13ms
ImportPowerShellClassesLarge2: 11ms
ImportPowerShellClassesLarge3: 12ms

ImportInlineCSharp1: 764ms
ImportInlineCSharp2: 25ms
ImportInlineCSharp3: 36ms
ImportInlineCSharpLarge1: 230ms
ImportInlineCSharpLarge2: 55ms
ImportInlineCSharpLarge3: 28ms

ImportCSharpAssembly1: 10ms
ImportCSharpAssembly2: 9ms
ImportCSharpAssembly3: 8ms
ImportCSharpAssemblyLarge1: 8ms
ImportCSharpAssemblyLarge2: 8ms
ImportCSharpAssemblyLarge3: 8ms
```

Interestingly, the first import of each method is always the slowest, presumably due to required assemblies being loaded into memory the first time they are needed.

We can see that using a C# assembly is the fastest method, followed by PowerShell classes, and then inline C# classes.

The test was also performed by running the import statements in different orders to test if one type of import would impact the performance of another.
The results were always the same, so the order of imports does not seem to matter.