Skip to content

Instantly share code, notes, and snippets.

@bokutin
Created June 29, 2017 06:14
Show Gist options
  • Save bokutin/7feaa87222aa960d79f198ec1ca0c699 to your computer and use it in GitHub Desktop.
Save bokutin/7feaa87222aa960d79f198ec1ca0c699 to your computer and use it in GitHub Desktop.
気になったので雑なの CPU時間 実時間
#!/usr/local/bin/perl
use Modern::Perl;
use Data::Section::Simple qw(get_data_section);
use File::Temp;
my $langs = get_data_section;
for my $lang (sort keys %$langs) {
my $code = $langs->{$lang};
my $tmp = File::Temp->new;
print $tmp $code;
close $tmp;
chmod 0755, "$tmp";
say "--> $lang";
my $out = `time -l $tmp 2>&1`;
die unless $?>>8 == 0;
say $out;
}
my @cmd = (
"uname -a",
"pkg info | ack 'p5-DBD-Pg|psycopg2|rubygem-pg|pgsql|^ruby-|^perl|^python'",
);
for (@cmd) {
say "--> $_";
system($_);
say "";
}
__DATA__
@@ perl
#!/usr/local/bin/perl
use Modern::Perl;
use DBI;
use Time::HiRes qw(gettimeofday tv_interval);
my $id = 1234;
my $dbh = DBI->connect("dbi:Pg:dbname=dbname", 'user', 'pass');
my $sth = $dbh->prepare('SELECT * FROM product WHERE id = ?');
my @cols;
$sth->execute($id) or die;
$sth->bind_columns( map { \$cols[$_] } 0..28 );
my $start = [gettimeofday];
for (1..100000) {
$sth->execute($id);
$sth->fetch;
die unless $cols[0] == $id;
}
say tv_interval($start);
@@ python
#!/usr/local/bin/python2.7
import psycopg2
import timeit
conn = psycopg2.connect("dbname=dbname user=user") # Is psycopg2 not supporting prepared?
id = 1234
cur = conn.cursor()
if __name__ == '__main__':
from time import time
cur.execute("SELECT * FROM product WHERE id = %s", [id])
start = time()
for i in range(0,100000):
cur.execute("SELECT * FROM product WHERE id = %s", [id])
cols = cur.fetchone()
if cols[0] != id:
raise
print time() - start
@@ ruby
#!/usr/local/bin/ruby
require 'pg'
db_conig = {
host: '127.0.0.1',
user: 'user',
password: 'pass',
dbname: 'dbname',
port: '5432'
}
connection = PG.connect(db_conig)
connection.prepare("select", 'SELECT * FROM product WHERE id = $1');
result = connection.exec_prepared('select', [1234])
start = Time.now
100000.times{
result = connection.exec_prepared('select', [1234])
val = result.values[0][0].to_i
if val != 1234 then
fail val
end
}
puts Time.now - start
@@ php
#!/usr/local/bin/php
<?php
$con = pg_connect("port=5432 user=user password=pass dbname=dbname");
$result = pg_prepare($con, "my_query", 'SELECT * FROM product WHERE id = $1');
$result = pg_execute($con, "my_query", array(1234));
$start_time = microtime(TRUE);
for ($count = 1; $count <= 100000; $count++) {
$result = pg_execute($con, "my_query", array(1234));
$row = pg_fetch_row($result);
if ( $row[0] != 1234 ) {
die;
}
}
print microtime(TRUE)-$start_time . "\n";
?>
__END__
--> perl
4.034649
4.06 real 0.66 user 0.28 sys
11796 maximum resident set size
7 average shared memory size
3 average unshared data size
113 average unshared stack size
1432 page reclaims
0 page faults
0 swaps
4 block input operations
0 block output operations
100005 messages sent
100004 messages received
0 signals received
100006 voluntary context switches
4 involuntary context switches
--> php
3.8969540596008
3.90 real 0.50 user 0.27 sys
8180 maximum resident set size
3417 average shared memory size
153 average unshared data size
108 average unshared stack size
508 page reclaims
0 page faults
0 swaps
4 block input operations
0 block output operations
100004 messages sent
100003 messages received
0 signals received
100005 voluntary context switches
1 involuntary context switches
--> python
10.5168910027
10.55 real 3.42 user 0.30 sys
15904 maximum resident set size
4 average shared memory size
4 average unshared data size
128 average unshared stack size
2603 page reclaims
0 page faults
0 swaps
2 block input operations
0 block output operations
100004 messages sent
100003 messages received
0 signals received
100005 voluntary context switches
26 involuntary context switches
--> ruby
4.696169619
4.74 real 0.77 user 0.38 sys
16932 maximum resident set size
3 average shared memory size
3 average unshared data size
124 average unshared stack size
2766 page reclaims
0 page faults
0 swaps
6 block input operations
0 block output operations
100005 messages sent
100004 messages received
0 signals received
100010 voluntary context switches
15 involuntary context switches
--> uname -a
FreeBSD 11.0-RELEASE-p9 FreeBSD 11.0-RELEASE-p9 #0: Tue Apr 11 08:48:40 UTC 2017 [email protected]:/usr/obj/usr/src/sys/GENERIC amd64
--> pkg info | ack 'p5-DBD-Pg|psycopg2|rubygem-pg|pgsql|^ruby-|^perl|^python'
p5-DBD-Pg-3.6.0 Provides access to PostgreSQL databases through the DBI
perl5-5.24.1_1 Practical Extraction and Report Language
php71-pgsql-7.1.5 The pgsql shared extension for php
py27-psycopg2-2.7.1 High performance Python adapter for PostgreSQL
python27-2.7.13_3 Interpreted object-oriented programming language
ruby-2.3.4,1 Object-oriented interpreted scripting language
rubygem-pg-0.20.0 Ruby interface to PostgreSQL library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment