Skip to content

Instantly share code, notes, and snippets.

@marvin
Created June 7, 2012 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/2887560 to your computer and use it in GitHub Desktop.
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
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 }
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