Archive
Posts Tagged ‘programming’
3 Substring methods benchmark
June 23, 2011
Leave a comment
#!/usr/bin/perl
use strict;
use warnings;
my $number='12345678';
my ($first,$second);
for my $i (1..10_000_000) {
if( $number =~ /^(\d{3}(\d{5}))$/ ) {
$first=$1;
$second=$2;
}
}
time perl reg.pl
real 0m14.066s
user 0m13.974s
sys 0m0.012s
============================================================
#!/usr/bin/perl
use strict;
use warnings;
my $number='12345678';
my ($first,$second);
for my $i (1..10_000_000) {
($first, $second)=unpack('a3a5', $number);
}
time perl unpack.pl
real 0m9.427s
user 0m9.321s
sys 0m0.016s
============================================================
#!/usr/bin/perl
use strict;
use warnings;
my $number='12345678';
my ($first,$second);
for my $i (1..10_000_000) {
$first=substr($number, 0, 3);
$second=substr($number, 3, 5);
}
time perl substr.pl
real 0m5.945s
user 0m5.846s
sys 0m0.017s
Seems the winner is the good old substr routine.
Categories: benchmarking, Programming
Perl, programming
Using mysql transaction
May 10, 2011
2 comments
There are times when mysql tables need to be truncated (or certain rows need to be removed) before new records are inserted, transaction statement should be used to ensure data integrity.
Example:
// blah blah establish mysql connection
mysql_query('START TRANSACTION');
$res1=mysql_query('TRUNCATE TABLE tbl_test');
// insert new rows into tbl_test, if all went well
$res2=true; // or false if something goes wrong
If ( $res1 && $res2 ) {
mysql_query('COMMIT');
} else {
mysql_query('ROLLBACK');
}
// blah blah close connection
Note: transaction feature is only available when InnoDB engine is used.
Categories: mysql, Programming
mysql, Php, programming