Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test if division is faster then multiplying and substracting
- function benchmark(){
- // all variables will be created before testing
- var i = 0;
- var result = 0;
- var date = 0;
- var time1 = 0;
- var time2 = 0;
- var end = 0;
- var start = 0;
- var rounds = 20; // how many times run the test for average time?
- var tries = 10000000; // how many times run the one computation?
- for (var n = 0; n<rounds;n++){
- result = 0;
- // save start date
- date = new Date();
- start = date.getMilliseconds();
- // compute
- for(i; i<tries;i++){
- result = 10/2;
- }
- // save end date
- date = new Date();
- end = date.getMilliseconds();
- time1+=end-start; // save used time
- // reset variables
- i = 0;
- result = 0;
- // save start date
- date = new Date();
- start = date.getMilliseconds();
- // compute
- for(i; i<tries;i++){
- result = 10*2;
- }
- // save end date
- date = new Date();
- end = date.getMilliseconds();
- // save used date
- time2 += end-start;
- }
- // show result
- console.log("Divising took "+time1/rounds+" (overal "+time1+") ms. Multiplying took "+time2/rounds+" (overal "+time2+") ms.")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement