Posted to tcl by lmcvoy at Sun Nov 25 20:12:26 GMT 2007view raw
- What is this? It's hand coded split in perl & tcl, vs the perl builtin split.
- The hand coded alg is coded as close to identically across both languages.
- The results are that the hand coded tcl is actually FASTER than perl but the
- perl builtin smokes everyone by a factor of 6 or more.
-
-
- travis /tmp/langbench time perl split.perl DATA
- 3647213
-
- real 0m47.545s
- user 0m47.443s
- sys 0m0.100s
- travis /tmp/langbench time perl split.pl DATA
- 3647213
-
- real 0m5.783s
- user 0m5.732s
- sys 0m0.052s
- travis /tmp/langbench time /home/lm/bk/bk/src/gui/bin/tclsh split.tcl DATA
- 3647213
-
- real 0m36.224s
- user 0m36.006s
- sys 0m0.112s
- travis /tmp/langbench cat split.perl
- sub wordsplit
- {
- chomp($_[0]);
- @list = ();
- $word = "";
- foreach $c (split(//, $_[0])) {
- if ($c =~ /\s/o) {
- push(@list, $word) if $word ne "";
- $word = "";
- } else {
- $word .= $c;
- }
- }
- push(@list, $word) if $word ne "";
- return @list;
- }
-
- $n = 0;
- while (<>) {
- @words = &wordsplit($_);
- $n += $#words + 1;
- }
- printf "%d\n", $n;
- travis /tmp/langbench cat split.pl
- $n = 0;
- while (<>) {
- @words = split;
- $n += $#words + 1;
- }
- printf "%d\n", $n;
- travis /tmp/langbench cat split.tcl
- proc wordsplit {str} {
- set list {}
- set word {}
- foreach char [split $str {}] {
- if {[string is space $char]} {
- if {[string length $word] > 0} {
- set list [lappend list $word]
- }
- set word {}
- } else {
- append word $char
- }
- }
- if {[string length $word] > 0} {
- set list [lappend list $word]
- }
- return $list
- }
-
- proc doit {file} {
- set f [open $file rb]
- set buf ""
- set n 0
- while {[gets $f buf] >= 0} {
- set words [wordsplit $buf]
- incr n [llength $words]
- }
- close $f
- return $n
- }
- set total 0
- foreach file $argv {
- incr total [doit $file]
- }
- puts $total
-