Advertisement
snake5

map-speed, the JS version

Dec 19th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function fmt_bytenum( n )
  3. {
  4.     var on = n;
  5.     var i = 0;
  6.     var units = [ "B", "kB", "MB" ];
  7.     while( n > 2048 && i < units.length - 1 )
  8.     {
  9.         n /= 1024;
  10.         i++;
  11.     }
  12.     return n + " " + units[i] + " (" + on + " bytes)";
  13. }
  14.  
  15. function printlns()
  16. {
  17.     for(var i = 0; i < arguments.length; i++)
  18.         console.log(arguments[i]);
  19. }
  20.  
  21. function ftime(){ return (new Date()).getTime() / 1000; }
  22. function sys_stat(){ return 0; }
  23.  
  24. var count = 100000;
  25.  
  26. mem_base = sys_stat(3);
  27. num_base = sys_stat(6);
  28. obj = new Map();
  29. t0 = ftime();
  30. for( var i = 0; i < count; ++i )
  31.     obj.set( i, ftime() );
  32. mem_load = sys_stat(3);
  33. num_load = sys_stat(6);
  34.  
  35. t1 = ftime();
  36. for( var i = 0; i < count; ++i )
  37.     dummy = obj.get( i );
  38.  
  39. t2 = ftime();
  40. for( var i = 0; i < count; ++i )
  41.     obj.delete( i );
  42. mem_unload = sys_stat(3);
  43. num_unload = sys_stat(6);
  44.  
  45. t3 = ftime();
  46.  
  47. obj = null;
  48. mem_free = sys_stat(3);
  49.  
  50. printlns( "-- info --", "count: " + count );
  51. printlns( "-- time --", "set: " + (t1-t0), "get: " + (t2-t1), "unset: " + (t3-t2) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement