YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Profiler (beta): Simple Profiling Example

# Profiler (beta): Simple Profiling Example

This example shows basic usage of the Profiler for profiling a single function. A single function is profiled and all of the information displayed.

### Note:

Keep an eye on the Logger console at right to see the profiling information being reported.

## Simple Profiling Example

This example begins by creating a namespace:

YAHOO.namespace("example.profiler");

This namespace serves as the core object upon which others will be added (to prevent creating global objects).

Next, an object is created with a method:

//object with method to profile YAHOO.example.profiler.MathHelper = { factorial : function (num){ if (num > 1) { return num * MathHelper.factorial(num-1); } else { return 1; } } };

This object, `MathHelper` contains a single method called `factorial()` that computes the factorial of a given number. Any time `factorial()` is called, the argument indicates how many times the function will be recursively called. For example, `factorial(10)` results in the funtion being called 10 times. This makes it an ideal test case for profiling because the results are so predictable.

### Registering the function

The most important step to profile this function is to call `registerFunction()` with the fully-qualified function name, which is `YAHOO.example.profiler.MathHelper`:

YAHOO.tool.Profiler.registerFunction("YAHOO.example.profiler.MathHelper.factorial");

Since this function is fully accessible in the global scope, there's no need to provide the owner object as the second argument.

### Running the example

With everything setup, the last step is to run the code. This initialization is assigned to take place when the document tree has been completely loaded by using the Event utility's `onDOMReady()` method:

YAHOO.util.Event.onDOMReady(function (){ YAHOO.example.profiler.MathHelper.factorial(10); var calls = YAHOO.tool.Profiler.getCallCount("YAHOO.example.profiler.MathHelper.factorial"); var max = YAHOO.tool.Profiler.getMax("YAHOO.example.profiler.MathHelper.factorial"); var min = YAHOO.tool.Profiler.getMin("YAHOO.example.profiler.MathHelper.factorial"); var avg = YAHOO.tool.Profiler.getAverage("YAHOO.example.profiler.MathHelper.factorial"); YAHOO.tool.Profiler.unregisterFunction("YAHOO.example.profiler.MathHelper.factorial"); YAHOO.log("Method YAHOO.example.profiler.MathHelper was run " + calls + "times."); YAHOO.log("The average time was " + avg + "ms."); YAHOO.log("The max time was " + max + " ms."); YAHOO.log("The min time was " + min + " ms."); });

The code block begins by calling `factorial()` once, which gets profiled. Then, the information about the function can be retrieved from the Profiler. This information is output into the Logger on the page, displaying the number of times that the function was called along with the minimum, maximum, and average running times. Since this is a very simple function, the run times will most likely be 0ms on most machines.

### YUI Logger Output:

### Profiler (beta) Examples:

- Simple Profiling Example
- Object Profiling Example

### More Profiler (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings