Last active
October 8, 2022 20:53
-
-
Save sebnozzi/dfbf6e133c904b55548bc304f877c4d1 to your computer and use it in GitHub Desktop.
Benchmark - Sum from halved random numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
st = time | |
result = 0 | |
counter = 0 | |
while counter < 3000000 | |
result = (result + rnd) / 2 | |
counter = counter + 1 | |
end while | |
print result | |
print "Took: " + round(time - st,2) + " seconds" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from random import random | |
st = datetime.now() | |
result = 0 | |
counter = 0 | |
while counter < 3000000: | |
result = (result + random()) / 2 | |
counter = counter + 1 | |
print(result) | |
print("Took: " + str(round((datetime.now() - st).total_seconds(), 2)) + " seconds") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
st = Time.now | |
result = 0 | |
counter = 0 | |
while counter < 3000000 | |
result = (result + rand) / 2 | |
counter = counter + 1 | |
end | |
e = Time.now | |
puts(result) | |
puts("Took: " + ((e-st).round(2)).to_s + " seconds") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Put this in some class or evaluate in a Workspace / Playground except for the return line" | |
run | |
| st et duration rnd result counter | | |
counter := 0. | |
result := 0. | |
rnd := Random new. | |
st := DateAndTime now. | |
[ counter < 3000000 ] whileTrue: [ | |
result := result + rnd next / 2. | |
counter := counter + 1 ]. | |
et := DateAndTime now. | |
duration := ((et - st) asMilliSeconds / 1000 round: 2) asFloat. | |
^ 'Took: ' , duration asString. |
As far as I know Python and Ruby (I might be wrong with Ruby) don't do JITting, Pharo most probably does (depends on the VM used).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my machine (MacBook Pro 2015) I get following results: