Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Last active October 8, 2022 20:53
Show Gist options
  • Save sebnozzi/dfbf6e133c904b55548bc304f877c4d1 to your computer and use it in GitHub Desktop.
Save sebnozzi/dfbf6e133c904b55548bc304f877c4d1 to your computer and use it in GitHub Desktop.
Benchmark - Sum from halved random numbers
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"
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")
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")
"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.
@sebnozzi
Copy link
Author

sebnozzi commented Oct 8, 2022

On my machine (MacBook Pro 2015) I get following results:

  • Python 3.10: 0.86 seconds
  • Ruby 2.6.3: 0.28 seconds
  • Pharo Smalltalk 10: 0.29 seconds
  • MiniScript v1.1: 9.07 seconds

@sebnozzi
Copy link
Author

sebnozzi commented Oct 8, 2022

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