-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pablo Villalba
committed
Mar 14, 2012
0 parents
commit 098fc01
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Underscore profile | ||
================== | ||
|
||
Benchmark your functions. | ||
|
||
Usage | ||
----- | ||
|
||
Wrap your functions with _.profile and call them normally. | ||
|
||
your_function = _.profile("Your Function Description", your_function); | ||
|
||
Whenever you want to see results, you can call: | ||
|
||
_.profileResults(); | ||
|
||
It will give you something like this: | ||
|
||
Thread.render: Called 18 times, average of 16 ms. Total: 0.304 s. | ||
|
||
Attributions | ||
------------ | ||
|
||
Made by Pablo Villalba (@micho) for Teambox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Underscore profile | ||
* | ||
* Benchmark your functions. | ||
* | ||
* Usage: Wrap your functions with _.profile and call them normally. | ||
* | ||
* your_function = _.profile("Your Function Description", your_function); | ||
* | ||
* Whenever you want to see results, you can call: | ||
* | ||
* _.profileResults(); | ||
* | ||
* It will give you something like this: | ||
* | ||
* Thread.render: Called 18 times, average of 16 ms. Total: 0.304 s. | ||
* | ||
*/ | ||
|
||
|
||
/** | ||
* Obtain a wrapped function with performance benchmarking | ||
* | ||
* @param {String} Name for this function. Will be used by the reports | ||
* @param {Function} Original function to be wrapped | ||
* | ||
* @return {Function} Wrapped function, which works just like the original one | ||
*/ | ||
_.profile = function (name, func) { | ||
_._profile_results = _._profile_results || {}; | ||
_._profile_results[name] = _._profile_results[name] || { | ||
times_called: 0, | ||
ms_total: 0 | ||
}; | ||
|
||
return _.wrap(func, function (f, a) { | ||
var result, start; | ||
start = Date.now(); | ||
result = f.apply(this, arguments); | ||
_._profile_results[name].times_called += 1; | ||
_._profile_results[name].ms_total += Date.now() - start; | ||
return result; | ||
}); | ||
}; | ||
|
||
/** | ||
* Display results from profiled functions. | ||
*/ | ||
_.profileResults = function () { | ||
_(_._profile_results).each(function (results, name) { | ||
console.log( | ||
name + ": " + | ||
"Called " + results.times_called + " times, " + | ||
"average of " + Math.floor(results.ms_total / results.times_called) + " ms. " + | ||
"Total: " + (results.ms_total / 1000).toFixed(3) + " s." | ||
); | ||
}); | ||
}; |