Created
June 7, 2012 08:58
-
-
Save marvin/2887560 to your computer and use it in GitHub Desktop.
WHY is the nonthreaded process faster inserting 10k data to mongo then 10threads inserting 1000 records
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 'rubygems' | |
require 'mongo' | |
require 'digest/md5' | |
@conn = Mongo::Connection.new | |
@db = @conn['testdb'] | |
@coll = @db['dummy'] | |
@coll.remove | |
startdate = Time.now | |
10000.times do |i| | |
checksum = Digest::MD5.hexdigest(i.to_s) | |
checksum2 = Digest::MD5.hexdigest(checksum) | |
@coll.insert({'a' => i+1, 'check' => checksum, 'check2' => checksum2}) | |
end | |
enddate = Time.now | |
subtime = enddate - startdate | |
puts "Final time: " + subtime.to_s | |
# puts "There are #{@coll.count} records. There they are:" | |
# @coll.find.each { |doc| puts doc.inspect } |
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 'rubygems' | |
require 'mongo' | |
require 'digest/md5' | |
@conn = Mongo::Connection.new | |
@db = @conn['testdb'] | |
@coll = @db['dummy'] | |
@coll.remove | |
startdate = Time.now | |
def insert(i) | |
2000.times do |j| | |
checksum = Digest::MD5.hexdigest(i.to_s + j.to_s) | |
checksum2 = Digest::MD5.hexdigest(checksum) | |
@coll.insert({'a' => i+j+1, 'check' => checksum, 'check2' => checksum2}) | |
end | |
end | |
@arr = [] | |
5.times do |i| | |
@arr[i] = Thread.new { | |
insert(i) | |
} | |
end | |
@arr.each {|t| t.join } | |
enddate = Time.now | |
subtime = enddate - startdate | |
puts "Final time: " + subtime.to_s | |
# puts "There are #{@coll.count} records. There they are:" | |
# @coll.find.each { |doc| puts doc.inspect } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment