Posted to tcl by dgp at Mon Apr 13 18:55:57 GMT 2015view raw

  1.  
  2. Testing with current Tcl trunk...
  3.  
  4. # demo.tcl
  5. # Call as: tclsh demo.tcl $length $copies $iters
  6.  
  7. lassign $argv length copies iters
  8. proc demo {length copies} {
  9. set original {}
  10. for {set i 0} {$i<$length} {incr i} {
  11. lappend original [list [tcl::mathfunc::rand]]
  12. }
  13. for {set i 0} {$i<$copies} {incr i} {
  14. set copy {}
  15. foreach record $original {
  16. lappend copy [list [lindex $record 0]]
  17. # lappend copy $record
  18. }
  19. set original $copy
  20. }
  21. }
  22. for {set i 0} {$i<$iters} {incr i} {
  23. puts [time {demo $length $copies}]
  24. }
  25.  
  26. $tclsh8.6 demo.tcl 10000 2000 8
  27. 6115335 microseconds per iteration
  28. 6491340 microseconds per iteration
  29. 7331890 microseconds per iteration
  30. 7902593 microseconds per iteration
  31. 7998689 microseconds per iteration
  32. 8280205 microseconds per iteration
  33. 8421163 microseconds per iteration
  34. 8749917 microseconds per iteration
  35.  
  36. Mystery is why does performance degrade?
  37.  
  38. Switch to the commented line in the inner loop
  39. and the degradation goes away:
  40.  
  41. $tclsh demo.tcl 10000 2000 8
  42. 2823475 microseconds per iteration
  43. 2867695 microseconds per iteration
  44. 2839149 microseconds per iteration
  45. 2887773 microseconds per iteration
  46. 2875267 microseconds per iteration
  47. 2883500 microseconds per iteration
  48. 2879356 microseconds per iteration
  49. 2891140 microseconds per iteration
  50.  
  51. Switch to a --disable-threads build, and
  52. the degradation goes away:
  53.  
  54. $tclsh demo.tcl 10000 2000 8
  55. 6257281 microseconds per iteration
  56. 6218093 microseconds per iteration
  57. 6194935 microseconds per iteration
  58. 6193332 microseconds per iteration
  59. 6197974 microseconds per iteration
  60. 6233841 microseconds per iteration
  61. 6207849 microseconds per iteration
  62. 6217702 microseconds per iteration
  63.  
  64. My primary suspect is zippy, but other hypotheses are invited.
  65.  
  66. Why does this happen? Does it happen for you too?