Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Last active April 20, 2024 18:24
Show Gist options
  • Save jhawthorn/ae84ee2723ff4930344778cfe40205a2 to your computer and use it in GitHub Desktop.
Save jhawthorn/ae84ee2723ff4930344778cfe40205a2 to your computer and use it in GitHub Desktop.
require "sqlite3"
require "benchmark"
FileUtils.rm_f("test.db")
def open_db
db = SQLite3::Database.new "test.db"
end
def create_db
db = open_db
db.execute "BEGIN"
# Based on tool/speedtest.tcl from sqlite
db.execute <<-SQL
create table t2 (a INTEGER, b INTEGER, c VARCHAR(100));
SQL
1.upto(25000) do |i|
r = rand(500000)
db.execute "INSERT INTO t2 VALUES(#{i},#{r},'[number_name #{r}]');"
end
db.execute "COMMIT"
end
def run_test(db = open_db)
i = 0
while i < 10000
sql = <<~SQL
SELECT count(*), avg(b) FROM t2 WHERE b>=#{i} AND b<#{i + 1000};
SQL
db.execute(sql)
i += 100
end
end
def run_tests(n=10, threaded: false)
if threaded
n.times.map { Thread.new { run_test } }.map(&:join)
else
n.times { run_test }
end
end
create_db
run_test
puts "single threaded:"
puts Benchmark.realtime { run_tests(threaded: false) }
puts "multi threaded:"
puts Benchmark.realtime { run_tests(threaded: true) }
@jhawthorn
Copy link
Author

jhawthorn commented Apr 19, 2024

sqlite3-ruby master

$ bundle exec ruby test.rb
single threaded:
0.9432485040742904
multi threaded:
1.1921860249713063

With (incomplete, hacky) GVL release patch:

$ bundle exec ruby test.rb
single threaded:
0.927121008047834
multi threaded:
0.17125224182382226

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment