Skip to content

Instantly share code, notes, and snippets.

@nevir
Created June 24, 2012 23:25
Show Gist options
  • Select an option

  • Save nevir/2985474 to your computer and use it in GitHub Desktop.

Select an option

Save nevir/2985474 to your computer and use it in GitHub Desktop.
➜ foo git:(master) ./sequential.rb
Finished in 1.293349
➜ foo git:(master) ./sequential.py
Finished in 2.837504
➜ foo git:(master) ./threads.rb
Finished in 1.272466
➜ foo git:(master) ./threads.py
Finished in 3.681996
#!/usr/bin/env python
from time import time
COUNT = 50000000
def countdown(n):
while n > 0:
n -= 1
start = time()
countdown(50000000)
print("Finished in %f" % (time() - start))
#!/usr/bin/env ruby
COUNT = 50000000
count = proc do |n|
while n > 0 do
n -= 1
end
end
start = Time.now
count.call(COUNT)
puts "Finished in #{Time.now - start}"
#!/usr/bin/env python
from threading import Thread
from time import time
COUNT = 50000000
def countdown(n):
while n > 0:
n -= 1
start = time()
t1 = Thread(target=countdown, args=(COUNT // 2,))
t2 = Thread(target=countdown, args=(COUNT // 2,))
t1.start(); t2.start()
t1.join(); t2.join()
print("Finished in %f" % (time() - start))
#!/usr/bin/env ruby
COUNT = 50000000
count = proc do |n|
while n > 0 do
n -= 1
end
end
start = Time.now
t1 = Thread.new { count.call(COUNT / 2) }
t2 = Thread.new { count.call(COUNT / 2) }
t1.join; t2.join
puts "Finished in #{Time.now - start}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment