Posted to tcl by lmcvoy at Sun Nov 25 20:12:26 GMT 2007view raw

  1. What is this? It's hand coded split in perl & tcl, vs the perl builtin split.
  2. The hand coded alg is coded as close to identically across both languages.
  3. The results are that the hand coded tcl is actually FASTER than perl but the
  4. perl builtin smokes everyone by a factor of 6 or more.
  5.  
  6.  
  7. travis /tmp/langbench time perl split.perl DATA
  8. 3647213
  9.  
  10. real 0m47.545s
  11. user 0m47.443s
  12. sys 0m0.100s
  13. travis /tmp/langbench time perl split.pl DATA
  14. 3647213
  15.  
  16. real 0m5.783s
  17. user 0m5.732s
  18. sys 0m0.052s
  19. travis /tmp/langbench time /home/lm/bk/bk/src/gui/bin/tclsh split.tcl DATA
  20. 3647213
  21.  
  22. real 0m36.224s
  23. user 0m36.006s
  24. sys 0m0.112s
  25. travis /tmp/langbench cat split.perl
  26. sub wordsplit
  27. {
  28. chomp($_[0]);
  29. @list = ();
  30. $word = "";
  31. foreach $c (split(//, $_[0])) {
  32. if ($c =~ /\s/o) {
  33. push(@list, $word) if $word ne "";
  34. $word = "";
  35. } else {
  36. $word .= $c;
  37. }
  38. }
  39. push(@list, $word) if $word ne "";
  40. return @list;
  41. }
  42.  
  43. $n = 0;
  44. while (<>) {
  45. @words = &wordsplit($_);
  46. $n += $#words + 1;
  47. }
  48. printf "%d\n", $n;
  49. travis /tmp/langbench cat split.pl
  50. $n = 0;
  51. while (<>) {
  52. @words = split;
  53. $n += $#words + 1;
  54. }
  55. printf "%d\n", $n;
  56. travis /tmp/langbench cat split.tcl
  57. proc wordsplit {str} {
  58. set list {}
  59. set word {}
  60. foreach char [split $str {}] {
  61. if {[string is space $char]} {
  62. if {[string length $word] > 0} {
  63. set list [lappend list $word]
  64. }
  65. set word {}
  66. } else {
  67. append word $char
  68. }
  69. }
  70. if {[string length $word] > 0} {
  71. set list [lappend list $word]
  72. }
  73. return $list
  74. }
  75.  
  76. proc doit {file} {
  77. set f [open $file rb]
  78. set buf ""
  79. set n 0
  80. while {[gets $f buf] >= 0} {
  81. set words [wordsplit $buf]
  82. incr n [llength $words]
  83. }
  84. close $f
  85. return $n
  86. }
  87. set total 0
  88. foreach file $argv {
  89. incr total [doit $file]
  90. }
  91. puts $total
  92.