-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make DataFrame.from_activerecord faster (#468)
* Make Daru::DataFrame.from_activerecord faster * Avoid DataFrame#add_row in each block * Add a benchmark of from_activerecord * Fix rubocop error
- Loading branch information
1 parent
be470ab
commit 5e962f3
Showing
2 changed files
with
39 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
$:.unshift File.expand_path("../../lib", __FILE__) | ||
|
||
require 'benchmark' | ||
require 'daru' | ||
require 'sqlite3' | ||
require 'dbi' | ||
require 'active_record' | ||
|
||
db_name = 'daru_test.sqlite' | ||
FileUtils.rm(db_name) if File.file?(db_name) | ||
|
||
SQLite3::Database.new(db_name).tap do |db| | ||
db.execute "create table accounts(id integer, name varchar, age integer, primary key(id))" | ||
|
||
values = 1.upto(100_000).map { |i| %!(#{i},"name_#{i}",#{rand(100)})! }.join(",") | ||
db.execute "insert into accounts values #{values}" | ||
end | ||
|
||
ActiveRecord::Base.establish_connection("sqlite3:#{db_name}") | ||
ActiveRecord::Base.connection | ||
|
||
class Account < ActiveRecord::Base; end | ||
|
||
Benchmark.bm do |x| | ||
x.report("DataFrame.from_sql") do | ||
Daru::DataFrame.from_sql(ActiveRecord::Base.connection, "SELECT * FROM accounts") | ||
end | ||
|
||
x.report("DataFrame.from_activerecord") do | ||
Daru::DataFrame.from_activerecord(Account.all) | ||
end | ||
end | ||
|
||
FileUtils.rm(db_name) |
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