|
|
|
|
|
by waterhouse
4723 days ago
|
|
I see. That is good to know; I merely chose mutating global variables in a loop because I knew how to do that in both languages. (I am not very familiar with Python.) That is not the idiomatic way to do it in Arc, either. I would normally use a recursive function, like this: arc> (time:xloop (i 0 n 0) (if (> i 100000000) n (next (+ i 1) (+ n i))))
time: 9121 cpu: 9130 gc: 0 mem: 480 ; the times are in msec
5000000050000000
Or perhaps a "higher-order function": arc> (time:sum idfn 1 100000000)
time: 19889 cpu: 19908 gc: 0 mem: 1224
5000000050000000
Or use a deforestation macro that I wrote, which is closest to your Python example: arc> (time:thunkify:reduce + (range 1 100000000))
time: 17971 cpu: 17985 gc: 0 mem: 3592
5000000050000000
Also, here's what you can get by dropping into Racket: arc> (time:$:let loop ((i 0) (n 0)) (if (> i 100000000) n (loop (+ i 1) (+ n i))))
time: 402 cpu: 403 gc: 0 mem: 920
5000000050000000
I suppose Python has an analogue of that--dropping into C, or at least loading C libraries. Which Racket can do too. Mmm. |
|