Skip to content

Instantly share code, notes, and snippets.

@hidek
Created June 23, 2009 10:04
Show Gist options
  • Select an option

  • Save hidek/134462 to your computer and use it in GitHub Desktop.

Select an option

Save hidek/134462 to your computer and use it in GitHub Desktop.
use warnings;
use DBI;
use Benchmark qw(:all);
my $count = 100;
my $loop = 100;
cmpthese(
$count,
{
commit_each_insert => \&commit_each_insert,
commit_bulk_insert => \&commit_bulk_insert,
}
);
sub commit_each_insert {
unlink('test1.db');
my $dbh = DBI->connect('dbi:SQLite:dbname=test1.db');
$dbh->do(
"CREATE TABLE test (id int not null, name text not null, primary key(id))"
);
for (1 .. $loop) {
$dbh->do("INSERT INTO test(id, name) VALUES($_, 'name_$_')");
}
}
sub commit_bulk_insert {
unlink('test2.db');
my $dbh = DBI->connect('dbi:SQLite:dbname=test2.db');
$dbh->do(
"CREATE TABLE test (id int not null, name text not null, primary key(id))"
);
$dbh->do('BEGIN');
for (1 .. $loop) {
$dbh->do("INSERT INTO test(id, name) VALUES($_, 'name_$_')");
}
$dbh->do('COMMIT');
}
Rate commit_each_insert commit_bulk_insert
commit_each_insert 19.1/s -- -71%
commit_bulk_insert 65.8/s 245%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment