Advertisement
zopper

JS div/mult benchmark

Oct 12th, 2012
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // test if division is faster then multiplying and substracting
  2. function benchmark(){
  3.     // all variables will be created before testing
  4.         var i = 0;
  5.         var result = 0;
  6.         var date = 0;
  7.         var time1 = 0;
  8.         var time2 = 0;
  9.         var end = 0;
  10.         var start = 0;
  11.         var rounds = 20; // how many times run the test for average time?
  12.         var tries = 10000000; // how many times run the one computation?
  13.        
  14.         for (var n = 0; n<rounds;n++){
  15.                
  16.                 result = 0;
  17.         // save start date
  18.                 date = new Date();
  19.                 start = date.getMilliseconds();
  20.                
  21.         // compute
  22.                 for(i; i<tries;i++){
  23.                         result = 10/2;
  24.                 }
  25.  
  26.         // save end date
  27.                 date = new Date();
  28.                 end = date.getMilliseconds();
  29.                 time1+=end-start; // save used time
  30.                
  31.         // reset variables
  32.                 i = 0;
  33.                 result = 0;
  34.         // save start date
  35.                 date = new Date();
  36.                 start = date.getMilliseconds();
  37.  
  38.         // compute
  39.                 for(i; i<tries;i++){
  40.                         result = 10*2;
  41.                 }
  42.        
  43.         // save end date
  44.                 date = new Date();
  45.                 end = date.getMilliseconds();
  46.  
  47.         // save used date
  48.                 time2 += end-start;
  49.         }
  50.     // show result
  51.         console.log("Divising took "+time1/rounds+" (overal "+time1+") ms. Multiplying took "+time2/rounds+" (overal "+time2+") ms.")
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement