Created
June 29, 2009 20:36
-
-
Save ricardochimal/137783 to your computer and use it in GitHub Desktop.
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
require 'thread' | |
require 'benchmark' | |
N = 200000 | |
def competition(&block) | |
t1 = Thread.new { | |
for i in 1..N | |
block.call | |
end | |
} | |
t2 = Thread.new { | |
for i in 1..N | |
block.call | |
end | |
} | |
t1.join | |
t2.join | |
end | |
def lock_delete | |
File.open("lock_delete", "w").flock(File::LOCK_EX) | |
File.delete("lock_delete") | |
end | |
def lock_unlock | |
f = File.open("lock_unlock", "w") | |
f.flock(File::LOCK_EX) | |
f.flock(File::LOCK_UN) | |
end | |
Benchmark.bm do |x| | |
x.report("lock delete:") { for i in 1..N; lock_delete; end } | |
x.report("lock unlock:") { for i in 1..N; lock_unlock; end } | |
x.report("2 lock delete:") { competition { lock_delete rescue '' } } # have to rescue Errno::ENOENT | |
x.report("2 lock unlock:") { competition { lock_unlock } } | |
end |
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
user system total real | |
lock delete: 1.270000 2.300000 3.570000 ( 8.243337) | |
lock unlock: 1.060000 0.650000 1.710000 ( 4.001936) | |
2 lock delete: 2.820000 4.670000 7.490000 ( 17.281890) | |
2 lock unlock: 2.270000 1.480000 3.750000 ( 8.710531) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment