Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - Performance of PowerShell (v1.0) vs WSH (v5.6)

Reply
 
11-02-2009   #1
David Kriz


 
 

Performance of PowerShell (v1.0) vs WSH (v5.6)

I have problem with Performance of my script in PowerShell.
It is very slow.

For testing of performance I created 2 simple scripts (see below). First in
PowerShell and second in WSH.
Both do the same (Only 2 empty nested cycles).

* PowerShell version running for 367 seconds.
* WSH version running for 23 seconds.
Is it possible that PowerShell is about 16 times slower than WSH?
Or mistake is somewhere else?

* PowerShell-script is:
....
/------------------------------------8<----------------------------------\
$I = [int]
$S = [string]
$X = [int]
[int]$Limit = 10000
[datetime]$TimeStop = Get-Date
[datetime]$TimeStart = Get-Date

$S = "*** Limit is: {0:N0}" -f $Limit
Write-Host $S
$S = "*** Start at: {0:dd}:{0:MM}:{0:yyyy} {0:HH}:{0:mm}:{0:ss}, wait please
.... " -f $TimeStart
Write-Host $S

for ($I = 1; $I -le $Limit; $I++) {
$X = 0
While ($X -lt $Limit) {
$X++
}
}

$TimeStop = Get-Date
$TimeDuration = $TimeStop - $TimeStart

$S = "*** Stop at : {0:dd}:{0:MM}:{0:yyyy} {0:HH}:{0:mm}:{0:ss}." -f
$TimeStop
Write-Host $S
Write-Host "*** Duration: $TimeDuration"

\------------------------------------8<----------------------------------/
....

* and WSH-script is:
....
/------------------------------------8<----------------------------------\
option explicit
Dim I, Limit, S, TimeDuration, TimeStart, TimeStop, X

Limit = 10000
TimeStart = Now
TimeStop = Now

Wscript.Echo "*** Limit is: " & CStr(Limit)

Wscript.Echo "*** Start at: " & CStr(TimeStart) & ", wait please ..."

For I = 1 To Limit
X = 0
While X < Limit
X = X + 1
Wend
Next

TimeStop = Now
TimeDuration = TimeStop - TimeStart

Wscript.Echo "*** Stop at : " & CStr(TimeStop)
S = Right("0" & CStr(Day(TimeDuration)),2)
S = S & "." & Right("0" & CStr(Month(TimeDuration)),2)
S = S & "." & Right("0" & CStr(Year(TimeDuration)),2)
S = S & " " & Right("0" & CStr(Hour(TimeDuration)),2)
S = S & ":" & Right("0" & CStr(Minute(TimeDuration)),2)
S = S & ":" & Right("0" & CStr(Second(TimeDuration)),2) & " ("
Wscript.Echo "*** Duration: " & S & CStr(TimeDuration) & ")"
\------------------------------------8<----------------------------------/
....

P.S.
I ran this scripts repeatedly on the same computer:
* notebook lenovo ThinkPad T400,
* CPU=Intel Core 2 Duo, 2.40 GHz,
* RAM=1.94GB
* OS = Microsoft Windows XP + SP2 [Version 5.1.2600]



My System SpecsSystem Spec
11-02-2009   #2
Martin Zugec


 
 

Re: Performance of PowerShell (v1.0) vs WSH (v5.6)

In this case, you should compare your vbs to following code (sorry,
just quick & dirty rewrite):

$S = [string]
[int]$Limit = 10000
[datetime]$TimeStart = Get-Date


$S = "*** Limit is: {0:N0}" -f $Limit
Write-Host $S
$S = "*** Start at: {0:dd}:{0:MM}:{0:yyyy} {0:HH}:{0:mm}:{0:ss}, wait
please
.... " -f $TimeStart
Write-Host $S

While ($I -le $limit) {
$I++
$X = 0
While ($X -lt $Limit) {
$X++
}
}


$TimeDuration = $(Get-Date) - $TimeStart

$S = "*** Stop at : {0:dd}:{0:MM}:{0:yyyy} {0:HH}:{0:mm}:{0:ss}." -f
$TimeStop
Write-Host $S
Write-Host "*** Duration: $TimeDuration"


Notice that I use While also in Posh example.

Interesting thing is that when I tried it, Posh was much faster when
limit was lower (up to 1000), however got slower and slower than
vbscript afterwards.

Martin
My System SpecsSystem Spec
11-02-2009   #3
Martin Zugec


 
 

Re: Performance of PowerShell (v1.0) vs WSH (v5.6)

BTW you should check cmdlet Measure-Command
My System SpecsSystem Spec
11-02-2009   #4
David Kriz


 
 

Re: Performance of PowerShell (v1.0) vs WSH (v5.6)

Good idea!
It is more efficient.
Thank you very much.
;-)

Now my script looks like this:
/------------------------------------8<----------------------------------\
$I = [int]
$S = [string]
$X = [int]
[int]$Limit = 1000
[datetime]$TimeStart = Get-Date

$S = "*** Limit is: {0:N0}" -f $Limit
Write-Host $S
$S = "*** Start at: {0:dd}:{0:MM}:{0:yyyy} {0:HH}:{0:mm}:{0:ss}, wait please
.... " -f $TimeStart
Write-Host $S

Measure-command {
for ($I = 1; $I -le $Limit; $I++) {
$X = 0
While ($X -lt $Limit) {
$X++
}
}
}
\------------------------------------8<----------------------------------/


"Martin Zugec" <martin.zugec@newsgroup> wrote in message
news:24bdda70-cbb5-4589-8409-9c7ef8a9cf1b@newsgroup
Quote:

> BTW you should check cmdlet Measure-Command
My System SpecsSystem Spec
11-03-2009   #5
Martin Zugec


 
 

Re: Performance of PowerShell (v1.0) vs WSH (v5.6)

Another way (the one I use quite often for benchmarking, if I want to
test different approaches):

1.) Create few different scenarios - in your case, create Test.ps1 and
Test.vbs

2.) From Powershell session, run each one of this script using Measure-
Command

Martin
My System SpecsSystem Spec
11-03-2009   #6
Martin Zugec


 
 

Re: Performance of PowerShell (v1.0) vs WSH (v5.6)

+ 1 more thing... You should change both scripts to support -Limit
argument. Then you can automate test and see what will be difference
in execution time.

As I said, I spend just few minutes on this, however I could see that
Posh is much faster with lower limit and vbscript is much faster once
limit get higher. It would be nice to see some graph or something

If you (for whatever reason) don't want to spend too much time with
it, let me know and I will write blog post about it

Martin
My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Performance of PowerShell (v1.0) vs WSH (v5.6)
Thread Forum
An obversation on Powershell performance PowerShell
Please share some tips on powershell performance - ctp2 PowerShell
PowerShell performance PowerShell
Performance Issues with PowerShell PowerShell
Slow performance initializing PowerShell from managed code PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd