#!/usr/bin/perl -w use Benchmark('timethese'); use Inline C; my $tststr="abcdefghijklmnopqrstuvwxyz01234567890\n"; my $dta=($tststr x 2000).( ($tststr."\n") x 1900).("\n" x 200); my %cnt=(); my $_tr = sub { my $cnt=$dta=~tr/\n//; $cnt{_tr}=$cnt; }; my $_m_while = sub { my $cnt=0; my $rcdsep="\n"; while ($dta=~m/$rcdsep/g) {$cnt++}; $cnt{_m_while}=$cnt; }; my $_m_for = sub { my $cnt=0; my $rcdsep="\n"; $cnt++ for($dta=~m/$rcdsep/g); $cnt{_m_for}=$cnt; }; my $_m_array = sub { my $cnt=0; my $rcdsep="\n"; $cnt=scalar(@{[$dta=~m/$rcdsep/g]}); $cnt{_m_array}=$cnt; }; my $_m_while_M = sub { my $cnt=0; my $rcdsep="\n\n"; while ($dta=~m/$rcdsep/g) {$cnt++}; $cnt{_m_while_M}=$cnt; }; my $_m_for_M = sub { my $cnt=0; my $rcdsep="\n\n"; $cnt++ for($dta=~m/$rcdsep/g); $cnt{_m_for_M}=$cnt; }; my $_m_array_M = sub { my $cnt=0; my $rcdsep="\n\n"; $cnt=scalar(@{[$dta=~m/$rcdsep/g]}); $cnt{_m_array_M}=$cnt; }; my $_c_strncmp = sub { my $rcdsep="\n"; my $cnt=c_strncmp($dta,$rcdsep); $cnt{_c_strncmp}=$cnt; }; my $_c_strncmp_M = sub { my $rcdsep="\n\n"; my $cnt=c_strncmp($dta,$rcdsep); $cnt{_c_strncmp_M}=$cnt; }; my $_c_strstr = sub { my $rcdsep="\n"; my $cnt=c_strstr($dta,$rcdsep); $cnt{_c_strstr}=$cnt; }; my $_c_strstr_M = sub { my $rcdsep="\n\n"; my $cnt=c_strstr($dta,$rcdsep); $cnt{_c_strstr_M}=$cnt; }; $_tr->(); # seed it?? timethese(300, { tr => $_tr, m_while => $_m_while, m_for => $_m_for, m_array => $_m_array, m_while_M => $_m_while_M, m_for_M => $_m_for_M, m_array_M => $_m_array_M, c_strncmp => $_c_strncmp, c_strncmp_M => $_c_strncmp_M, c_strstr => $_c_strstr, c_strstr_M => $_c_strstr_M } ); print "\n=== check counts ===\n"; printf("%18s %5d\n",$_,$cnt{$_}) for (keys %cnt); __END__ __C__ #include int c_strncmp(char * buf, char * sep){ int cnt = 0; int seplen = strlen(sep); for(; *buf ; buf++) if(!strncmp(buf,sep,seplen)){ cnt++; buf+=seplen-1; } return cnt; } int c_strstr(char * buf, char * sep){ int cnt = 0; int seplen = strlen(sep); for(; *buf; buf+=seplen, cnt++) buf=strstr(buf,sep); return cnt; } -- snip --