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

Vista - Question on Performance

Reply
 
Old 03-17-2007   #1 (permalink)


 
 

Question on Performance

I tried to solve a puzzle using PS, and the evaluation time was very long. (1000seconds+)
The algorithm is an adaptation from a Python Algorithm (which runs in appx 60 seconds):

To count number of quadruplets with sum = 0 - comp.lang.python | Google Groups

So my question is:
Why is the difference so big between PS ant Python in this particular case.
Here is the PS script.. (I used Generic Collection, because the standard hashtable was much much slower -(boxing I think..))

Code:
$t1=date;$a=@();$b=@();$c=@();$d=@();$s=0;$z=0
 
$base=[System.Collections.Generic.Dictionary``2];
$h= New-Object $base.MakeGenericType(([type] "int",[type] "int"))
 
gc C:\m4000.txt | % {$l=$_.Split();$a+=[int]$l[0];$b+=[int]$l[1];$c+=[int]$l[2];$d+=[int]$l[3]}
 
for($i=0;$i -lt 4000;$i++){for($j=0;$j -lt 4000;$j++){$h[$a[$i]+$b[$j]]+=1}}
for($i=0;$i -lt 4000;$i++){for($j=0;$j -lt 4000;$j++){$s+=$h[-$c[$i]-$d[$j]]}}
 
$t2=date; $t=$t2.Subtract($t1).TotalSeconds
 
"Answer=" + $s + ", Time=" +$t
 
Answer=0, Time=1050.703125 (seconds)
Regards,
rockmoose


Last edited by z3r010; 03-18-2007 at 01:49 PM.. Reason: spelling
My System SpecsSystem Spec
Old 03-18-2007   #2 (permalink)


 
 

Btw, here is the php code I ran on the same machine.
(The algorithm is the same as far as I can tell...)
There must be some fundamental difference in how the code is evaluated.
(To account for the time difference)

PS=1000 sec, PHP=42 sec on my machine.
Maybe I am just comparing apples and oranges?

Any ideas?

rockmoose

Code:
import time 
 
t = time.clock()
 
q,w,e,r,sch,h = [],[],[],[],0,{} 
f = open("C:/m4000.txt","rt")
 
for o in range(4000): 
    row = map(int, f.readline().split()) 
    q.append(row[0]) 
    w.append(row[1]) 
    e.append(row[2]) 
    r.append(row[3])
f.close()
 
for x in q: 
    for y in w: 
        if (x+y) in h: 
            h[x+y] += 1 
        else: 
            h[x+y] = 1
 
for x in e: 
    for y in r: 
        if -(x+y) in h: 
            sch += h[-(x+y)] 
 
q,w,e,r,h = None,None,None,None,None 
print sch 
print time.clock() - t,"secs"
 
>>> ================================ RESTART ================================
>>> 
0
42.0711987577 secs

Last edited by z3r010; 03-18-2007 at 01:49 PM.. Reason: spelling
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copying Files in Vista Performance Question Vista General
General Performance Question Vista performance & maintenance
performance, compatibility question Vista performance & maintenance
Connections, performance question Vista performance & maintenance
Simple WMP11 Performance Question Vista General


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46