1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#Functions [javscript] var ms = 0; var state = 0; function startstop() { if (state == 0) { state = 1; then = new Date(); then.setTime(then.getTime() - ms); } else { state = 0; now = new Date(); ms = now.getTime() - then.getTime(); } } function swreset() { state = 0; ms = 0; } function display() { now = new Date(); ms = now.getTime() - then.getTime(); document.write('Time:'+ ms); } //Array Join startstop(); str1 = ''; arr = []; for (var i = 0; i < 100000; i++) { arr[i] = ' AND A ' + i; } str1 = arr.join(' '); startstop(); display(); swreset();
Refactorings
No refactoring yet !
KangOl
October 27, 2007, October 27, 2007 15:04, permalink
using firebug with the methods console.time(name) and console.timeEnd(name)
Andre Steenveld
October 29, 2007, October 29, 2007 10:52, permalink
Just made a little class out of the stop watch.
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
var StopWatch = function( startOnCreation ){ this.start = function(){ startTime = new Date( endTime === null ? 0 : endTime ); endTime = null; }; this.stop = function(){ endTime = startTime.getTime(); startTime = null; }; this.startStop = function(){ startTime === null ? this.startWatch() : this.stopWatch(); }; this.getTime = function(){ return ( startTime !== null ? startTime.getTime() : endTime ).toString(); }; this.reset = function( startOnReset ){ endTime = null; if( !!startOnReset ){ this.startWatch(); } }; var startTime = null, endTime = null; this.reset( startOnCreation ); }; StopWatch.prototype.toString = function( ){ return this.getTime().toString(); }; // --- Example 0 --- var myStopWatch = new StopWatch(); myStopWatch.start(); // // Do some complicated stuff... // myStopWatch.stop(); print( "Time passed [ " + myStopWatch + "ms ]" ); // asuming we have a print method // --- Example 1 --- myStopWatch.reset(); // recycle the first instance // // Do come complicated stuff... // print( "[ " + myOtherStopWatch + "ms ] have passed." ); myOtherStopWatch.stop(); print( "A total of [ " + myOtherStopWatch + "ms ] have passed." );
vladimir prieto
November 5, 2007, November 05, 2007 00:06, permalink
sometime ago i made something very similar using mootools. i upload an example of this on
http://vladimir.akilles.cl/scripts/mooTimer
hope helps you in some way...cu
I use this to test speed of javascript code. Any ideas on how to make it better?