Posted to tcl by daapp at Wed Sep 01 01:44:55 GMT 2010view raw

  1. namespace import tcl::mathop::*
  2.  
  3.  
  4. set repeat 500
  5.  
  6. set from 1
  7. set to 10000
  8.  
  9. proc nop {v} {}
  10.  
  11. puts [time {
  12. for {set i $from} {$i <= $to} {incr i} {
  13. + [* $i $i] [* 2 $i]
  14. }
  15. } $repeat]
  16.  
  17. puts [time {
  18. for {set i $from} {$i <= $to} {incr i} {
  19. expr {$i*$i+2*$i}
  20. }
  21. } $repeat]
  22.  
  23.  
  24. puts [time {
  25. for {set i $from} {$i <= $to} {incr i} {
  26. set a [+ [* $i $i] [* 2 $i]]
  27. }
  28. } $repeat]
  29.  
  30. puts [time {
  31. for {set i $from} {$i <= $to} {incr i} {
  32. set a [expr {$i*$i+2*$i}]
  33. }
  34. } $repeat]
  35.  
  36.  
  37.  
  38. puts [time {
  39. for {set i $from} {$i <= $to} {incr i} {
  40. nop [+ [* $i $i] [* 2 $i]]
  41. }
  42. } $repeat]
  43.  
  44. puts [time {
  45. for {set i $from} {$i <= $to} {incr i} {
  46. nop [expr {$i*$i+2*$i}]
  47. }
  48. } $repeat]
  49.  
  50. ### result
  51. # 8720.0 microseconds per iteration
  52. # 8874.0 microseconds per iteration
  53. # 10000.0 microseconds per iteration
  54. # 10188.0 microseconds per iteration
  55. # 13062.0 microseconds per iteration
  56. # 12876.0 microseconds per iteration
  57.