![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #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)
rockmoose Last edited by z3r010; 03-18-2007 at 01:49 PM.. Reason: spelling |
My System Specs![]() |
| | #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 Specs![]() |
![]() |
| 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 | |||