Trying to demystify PowerShell conditional statements processing performance

Recently, I was drafting some PowerShell codes and stumbled across which conditional statement should I use for a very simple task and decided to explore abit more on their performance differences.

Below is a sample code that I have coded to display the result of the overall performance between some of the conditional statements method and it is purely for educational coding purposes.

If anyone is interested to find out, copy the codes below, paste it on PowerShell ISE and run it.

<#

.SYNOPSIS
    Demystify PowerShell conditional statements processing performance and return
    the processing result in term of milliseconds.

.DESCRIPTION
    Automatically generate a random number between 0 to 10, loop the execution 
    1000 times and parses the random number to a series of conditional statements 
    such as Switch, If/Else or HashTable and to output an average of the processing 
    performance in term of milliseconds.

.NOTES
    This is meant for coding educational purposes on the differences between conditional
    statements and how you could measure your code performance.

    Author  : Ryen Kia Zhi Tang 
    Date    : 01/06/2016
    Email   : ryentang.wordpress.com
    Version : 1.0

.LINK
    Microsoft TechNet - about_Switch: https://technet.microsoft.com/en-us/library/hh847750.aspx
    Microsoft TechNet - about_If: https://technet.microsoft.com/en-us/library/hh847876.aspx
    Microsoft TechNet - about_Hash_Tables: https://technet.microsoft.com/en-us/library/hh847780.aspx
    Windows PowerShell Tip - Using the Switch Statement: https://technet.microsoft.com/en-us/library/ff730937.aspx
    Windows PowerShell Tip - Working with Hash Tables: https://technet.microsoft.com/en-us/library/ee692803.aspx

#>

# Clear Screen
Clear-Host ;

# Specify the DoLoop value
$SpecifyDoLoopValue = 1000 ;

################################## Begin the Do Loop ##################################
Do {

# Increment DoLoop counter
$DoLoopCounter++

# Print a New Line Break
Write-Host "`n" ;

# Generate a random number between 0 to 10
$Random = 0..10 | `
    Get-Random ;




################ Switch Conditional Statement - Start ################

# Calculate Switch conditional statement in validating 
#  the random number and return the total milliseconds
$TotalMilliSecondsPerExecution = ((Measure-Command { `
    Switch($Random) { `
        0  {} ; `
        1  {} ; `
        2  {} ; `
        3  {} ; `
        4  {} ; `
        5  {} ; `
        6  {} ; `
        7  {} ; `
        8  {} ; `
        9  {} ; `
        Default {} ; `
    } ;
}).TotalMilliseconds) ;

################# Switch Conditional Statement - End #################



# Print Switch Total Milliseconds per run
Write-Host "Switch($DoLoopCounter) total milliseconds per execution = $TotalMilliSecondsPerExecution" ;

# Store Switch Total Milliseconds per run result
$SwitchTotalMilliSeconds += $TotalMilliSecondsPerExecution ;



################ If/Else Conditional Statement - Start ###############

# Calculate If/Else condition function in validating 
#  the random number and return the total milliseconds
$TotalMilliSecondsPerExecution = (Measure-Command { `
    If($Random -eq 0) { } `
    ElseIf($Random -eq 1) { } `
    ElseIf($Random -eq 2) { } `
    ElseIf($Random -eq 3) { } `
    ElseIf($Random -eq 4) { } `
    ElseIf($Random -eq 5) { } `
    ElseIf($Random -eq 6) { } `
    ElseIf($Random -eq 7) { } `
    ElseIf($Random -eq 8) { } `
    ElseIf($Random -eq 9) { } `
    Else { } ;
}).TotalMilliseconds

################# If/Else Conditional Statement - End ################



# Print If/Else Total Milliseconds per run
Write-Host "If/Else($DoLoopCounter) total milliseconds per execution = $TotalMilliSecondsPerExecution" ;

# Store IfElse Total Milliseconds per run result
$IfElseTotalMilliSeconds += $TotalMilliSecondsPerExecution ;



############### HashTable Conditional Statement - Start ##############

$HashTable = @{
    0 = { } ;
    1 = { } ;
    2 = { } ;
    3 = { } ;
    4 = { } ;
    5 = { } ;
    6 = { } ;
    7 = { } ;
    8 = { } ;
    9 = { } ;
    default = { } ;
}

# Calculate If/Else condition function in validating 
# the random number and return the total milliseconds
$TotalMilliSecondsPerExecution = (Measure-Command { `
    If($HashTable.ContainsKey($Random)){ `
        .$HashTable.$Random ;
    }Else{ `
        .$HashTable.default ;
    } ;
}).TotalMilliseconds ;

################ HashTable Conditional Statement - End ###############



# Print HashTable Total Milliseconds per run
Write-Host "HashTable($DoLoopCounter) total milliseconds per execution = $TotalMilliSecondsPerExecution" ;

# Store HashTable Total Milliseconds per run result
$HashTableTotalMilliSeconds += $TotalMilliSecondsPerExecution ;

# Keep running the Do Loop until 1000 executions
}While(!($DoLoopCounter -gt ($SpecifyDoLoopValue - 1))) ;
################################### End the Do Loop ###################################




# Sum up all the conditional statements total milliseconds
$AllConditionStatementsTotalMilliSeconds = ( `
    $SwitchTotalMilliSeconds + `
    $IfElseTotalMilliSeconds + `
    $HashTableTotalMilliSeconds) ;




#################################### Result Output ####################################
Write-Host "`n"
Write-Host "#####################  Total MilliSeconds Result  #####################" `
    -ForegroundColor Yellow ;
Write-Host "Total Switch Milliseconds = $SwitchTotalMilliSeconds" `
    -ForegroundColor Yellow ;
Write-Host "Total IfElse Milliseconds = $IfElseTotalMilliSeconds" `
    -ForegroundColor Yellow ;
Write-Host "Total HashTable Milliseconds = $HashTableTotalMilliSeconds" `
    -ForegroundColor Yellow ;
Write-Host "`n"
Write-Host "##################### Average MilliSeconds Result #####################" `
    -ForegroundColor Yellow ;
Write-Host "Average Switch Milliseconds = $($SwitchTotalMilliSeconds / 10)" `
    -ForegroundColor Yellow ;
Write-Host "Average IfElse Milliseconds = $($IfElseTotalMilliSeconds / 10)" `
    -ForegroundColor Yellow ;
Write-Host "Average HashTable Milliseconds = $($HashTableTotalMilliSeconds / 10)" `
    -ForegroundColor Yellow ;
Write-Host "`n"
Write-Host "################ Overall Efficiency Performance Result ################" `
    -ForegroundColor Green ;
Write-Host "Switch Performance Percentage =" ( `
    '{0:P0}' -f (1 - (`
        $SwitchTotalMilliSeconds / $AllConditionStatementsTotalMilliSeconds))) `
    -ForegroundColor Green ;
Write-Host "If/Else Performance Percentage =" ( `
    '{0:P0}' -f (1 - (`
        $IfElseTotalMilliSeconds / $AllConditionStatementsTotalMilliSeconds))) `
    -ForegroundColor Green ;
Write-Host "HashTable Performance Percentage =" ( `
    '{0:P0}' -f (1 - ( `
        $HashTableTotalMilliSeconds / $AllConditionStatementsTotalMilliSeconds))) `
    -ForegroundColor Green ;
#################################### Result Output ####################################



# Clear All Variables
Clear-Variable `
    -Name Random, `
    SpecifyDoLoopValue, `
    DoLoopCounter, `
    TotalMilliSecondsPerExecution, `
    SwitchTotalMilliSeconds, `
    IfElseTotalMilliSeconds, `
    HashTableTotalMilliSeconds,
    HashTable, `
    AllConditionStatementsTotalMilliSeconds ;

Advertisement