Reputation: 1
So in my JavaScript file I'm trying to get it to detect how long an encryption/decryption took to occur and I'm getting it linked but the time is off quite a bit this is the code,
var stop = new Date();
var time = (stop-start) % 1000;
if(time < 10) var ms = "00"+time;
else if((time < 100) && (time >= 10)) var ms = "0"+time;
else var ms = time;
var s = Math.floor((stop-start) / 1000);
if(document.forms['timer'].elements[0].checked == true)
document.forms['timer'].elements[1].value = s+":"+ms;
else document.forms['timer'].elements[1].value = "";
Which gives me answers like this 1327851955:962 as far as time it took when most of the time I'm sure it took less then a second, so if you don't mind could someone please explain how to fix it?
Upvotes: 0
Views: 1130
Reputation: 9593
if you need it only for debugging use the Google Chrome Dev Tools you have few really nice methods;
console.time('encryption')
where you want to start measuring the time and console.timeEnd('encryption')
where you want to finish it.
You can also use some other great console tools like for example the profile method
console.profile('encryption')
-> console.profileEnd('encryption')
then go to the Profiles tab to be able to debug the CPU performance of your code and find any bottlenecks and places for improvement
Upvotes: 3
Reputation: 10217
I don't quite get what your code is trying to acheive but you can time the amount of time it takes to do something like this:
var startTime = new Date().getTime();
//insert a call to do your encryption/decryption here..
var endTime = new Date().getTime();
console.log("The encryption/decryption took: " + (endTime - startTime) + "ms.");
Upvotes: 1