My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package net.babeuf.bench
{
import net.babeuf.babeuf;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
import flash.utils.Timer;

use namespace babeuf;

/**
* Class to test different functions and compare their execution time to do benchmarks
* @author damien pegoraro
*/
public class BenchMark extends Sprite
{
private var iterations : uint ;
private var tests : uint ;
private var functions : Array ;
private var l : uint ;
private var timer : Timer ;
private var output : TextField ;

/**
* Constructs a new BenchMark Instance
* @param iterations The <code>uint</code> number of iterations for each function in each loop
* @param tests The <code>uint</code> number of loops to build the average result
*/
public function BenchMark( iterations : uint = 10, tests : uint = 1 )
{
functions = new Array() ;
this.iterations = iterations ;
this.tests = tests ;
l = 0 ;

timer = new Timer(100, tests) ;
timer.addEventListener(TimerEvent.TIMER, run) ;
timer.addEventListener(TimerEvent.TIMER_COMPLETE,end) ;

output = new TextField() ;
output.defaultTextFormat = new TextFormat("_sans", 10,0) ;
output.border = true ;
output.background = true ;
output.backgroundColor = 0xC0C0C0 ;
output.wordWrap = true ;
output.multiline = true ;
output.width = 250 ;
output.height = 400 ;
addChild(output) ;
}


/**
* run the test
*/
public function start() : void
{
output.appendText("Starting "+ tests + " tests with " + iterations + " loops each \n") ;
timer.start() ;
}

private function run( e: TimerEvent) : void
{
var s : int = 0 ;
var s2 : int = 0 ;
var t : Test = null ;

output.appendText("-----------\n") ;
output.appendText("test " + timer.currentCount + "\n" ) ;
output.appendText("-----------\n") ;
output.scrollV = output.maxScrollV ;

for ( var j : uint = 0 ; j < l ; ++j )
{
t = functions[j] as Test ;

s = getTimer() ;

for ( var k : uint = 0 ; k < iterations ; ++k)
{
t.run() ;
}
s2 = getTimer() - s ;
t.addResult(s2) ;
output.appendText(t.id + " -> " + s2 + "ms\n") ;
output.scrollV = output.maxScrollV ;
}
}

private function end( e: TimerEvent ) : void
{
timer.reset() ;
output.appendText("-----------\n") ;
output.appendText("Average \n" ) ;
output.appendText("-----------\n") ;

for each ( var t : Test in functions )
{
output.appendText(t.id + " -> " + t.getAverage() + "ms\n") ;
output.scrollV = output.maxScrollV ;
t.reset() ;
}

}

/**
* Add a function to the current test
* @param func the Function you want to bench
* @param scope the Object where the function is
* @param id a name for the output ( default name will be function1, function2... )
* @param ...params parameters to give to the function
*/
public function addFunction( func : Function, scope : Object, id : String = "" , ...params) : void
{
var t : Test = new Test( func, scope, id == "" ? "function" + (l+1) : id, params) ;
functions.push(t) ;
l++ ;
}
}
}


internal class Test
{
public var func : Function ;
public var scope : Object ;
public var id : String ;
public var params : Array ;

private var s : int ;
private var results : Array ;

public function Test( func : Function, scope : Object, id : String, params : Array)
{
this.func = func ;
this.id = id ;
this.scope = scope ;
this.params = params ;

results = new Array() ;
}

public function run() : void
{
func.apply( scope, params) ;
}

public function reset() : void
{
results = new Array() ;
}

public function addResult( n : int ) : void
{
results.push(n) ;
}

public function getAverage() : int
{
var l : int = results.length ;
var s : int = 0 ;
for ( var i : uint = 0 ; i < l ; ++i )
{
s += (results[i] as uint) ;
}

return int(s / l);


}


}

Change log

r57 by Babeuf on Oct 27, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r53 by Babeuf on Sep 22, 2010   Diff
Documentation update and more cleaning
r52 by Babeuf on Sep 21, 2010   Diff
- Package form
- Documentation
- Big cleaning
r28 by Babeuf on Dec 23, 2009   Diff
[No log message]
All revisions of this file

File info

Size: 4290 bytes, 175 lines
Powered by Google Project Hosting