profile.nut 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // The Computer Language Benchmarks Game
  2. // http://benchmarksgame.alioth.debian.org/
  3. // contributed by Mike Pall
  4. local _sq_profile_calls_at, _sq_profile_calls, _sq_profile_total, _sq_profile_this,
  5. _sq_profile_start_time, _sq_profile_end_time;
  6. local function profileReset()
  7. {
  8. _sq_profile_calls_at = {};
  9. _sq_profile_calls = {};
  10. _sq_profile_total = {};
  11. _sq_profile_this = {};
  12. _sq_profile_start_time = 0;
  13. _sq_profile_end_time = 0;
  14. }
  15. profileReset();
  16. local function profileDebughook(event_type,sourcefile,line,funcname)
  17. {
  18. //local fname = format("%s:%d", funcname ? funcname : "unknown", line);
  19. local fname = funcname ? funcname : "unknown";
  20. local srcfile=sourcefile ? sourcefile : "unknown";
  21. local fname_at = format("%s:%d:%s", fname, line, srcfile);
  22. //local fname_at = fname + ":" + line + ":" + srcfile;
  23. switch (event_type) {
  24. //case 'l': //called every line(that contains some code)
  25. //::print("LINE line [" + line + "] func [" + fname + "]");
  26. //::print("file [" + srcfile + "]\n");
  27. //break;
  28. case 'c': //called when a function has been called
  29. //::print("LINE line [" + line + "] func [" + fname + "]");
  30. //::print("file [" + srcfile + "]\n");
  31. _sq_profile_calls_at.rawset(fname_at, _sq_profile_calls_at.rawget(fname_at, 0) + 1);
  32. _sq_profile_this.rawset(fname, os.clock());
  33. break;
  34. case 'r': //called when a function returns
  35. //::print("LINE line [" + line + "] func [" + fname + "]");
  36. //::print("file [" + srcfile + "]\n");
  37. local time = os.clock() - _sq_profile_this.rawget(fname, 0);
  38. _sq_profile_total.rawset(fname, _sq_profile_total.rawget(fname, 0) + time);
  39. _sq_profile_calls.rawset(fname, _sq_profile_calls.rawget(fname, 0) + 1);
  40. break;
  41. }
  42. }
  43. local function profileStart()
  44. {
  45. profileReset();
  46. _sq_profile_start_time = os.clock();
  47. setdebughook(profileDebughook);
  48. }
  49. local function profileEnd()
  50. {
  51. setdebughook(null);
  52. _sq_profile_end_time = os.clock();
  53. }
  54. local function profileDump()
  55. {
  56. // print the results
  57. local total_time = _sq_profile_end_time - _sq_profile_start_time;
  58. print(format("Profile info: %.3f seconds", total_time));
  59. local info_ary = [];
  60. foreach( fname, time in _sq_profile_total )
  61. {
  62. if(fname == "profileStart" || fname == "profileEnd") continue;
  63. local relative_time = time / (total_time / 100.0);
  64. local rt_int = relative_time.tointeger();
  65. local rt_frac = ((relative_time - rt_int) * 100).tointeger();
  66. info_ary.append(format("%02d.%02d %% in %.3f seconds after %d calls to %s", rt_int, rt_frac, time, _sq_profile_calls.rawget(fname, 0), fname));
  67. }
  68. info_ary.sort(@(a,b) a<b ? 1 : (a>b ? -1 : 0));
  69. foreach(line in info_ary)
  70. {
  71. print(line);
  72. }
  73. info_ary.clear();
  74. foreach( fname, count in _sq_profile_calls_at )
  75. {
  76. if(fname.startswith("profileStart") || fname.startswith("profileEnd")) continue;
  77. info_ary.append(format("%6d\tcalls to %s", count, fname));
  78. }
  79. info_ary.sort(@(a,b) a<b ? 1 : (a>b ? -1 : 0));
  80. foreach(line in info_ary)
  81. {
  82. print(line);
  83. }
  84. }
  85. profileStart();
  86. local function BottomUpTree(item, depth){
  87. if (depth > 0){
  88. local i = item + item;
  89. --depth;
  90. return [ item, BottomUpTree(i-1, depth), BottomUpTree(i, depth) ];
  91. }
  92. return [ item ];
  93. }
  94. local function ItemCheck(tree){
  95. if (tree.get(1, false)) return tree[0] + ItemCheck(tree[1]) - ItemCheck(tree[2])
  96. return tree[0]
  97. }
  98. local start = os.clock()
  99. //check_delayed_release_hooks(false);
  100. local N = vargv.get(1, 4).tointeger();
  101. local mindepth = 4
  102. local maxdepth = mindepth + 2
  103. if (maxdepth < N) maxdepth = N
  104. {
  105. local stretchdepth = maxdepth + 1
  106. local stretchtree = BottomUpTree(0, stretchdepth)
  107. print(format("stretch tree of depth %d\t check: %d",
  108. stretchdepth, ItemCheck(stretchtree)))
  109. }
  110. local longlivedtree = BottomUpTree(0, maxdepth)
  111. for(local depth=mindepth; depth <= maxdepth; depth += 2){
  112. local iterations = math.pow(2, (maxdepth - depth + mindepth)).tointeger()
  113. local check = 0
  114. for(local i=0; i < iterations; ++i){
  115. check += ItemCheck(BottomUpTree(1, depth)) +
  116. ItemCheck(BottomUpTree(-1, depth))
  117. }
  118. print(format("%d\t trees of depth %d\t check: %d",
  119. iterations*2, depth, check))
  120. }
  121. print(format("long lived tree of depth %d\t check: %d\n",
  122. maxdepth, ItemCheck(longlivedtree)))
  123. local function printResult()
  124. {
  125. print("binary-tree", N, os.clock()-start)
  126. }
  127. printResult();
  128. profileEnd();
  129. profileDump();