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
<?php
/**
 * Coin flip accuracy test.
 * by Joel Larson ([email protected])
 *
 * Created as a benchmark for testing CPU, but it's not very intensive.
 */

// Total trials.
$flipcount = 20000000;

// Preset variables for head/tails
$headcount = 0;
$tailcount = 0;

// INIT counter.
$i = 0;

// Start the benchmark.
$time_start = microtime(true);

// Run the trial!
while ($i++ < $flipcount) {
    1 === mt_rand(1,2) ? $headcount++ : $tailcount++;
}

// End the benchmark.
$time_end = microtime(true);

unset($i);

// The output!
$output = <<<EOFILE
Heads: %s (%s%%)<br />
Tails: %s (%s%%)<br />
Total Time: %s seconds.
EOFILE;

// Throw it out at the browser.
echo sprintf(
    $output,                     // The output.
    $headcount,                  // Total head count.
    (100*$headcount/$flipcount), // Head count percentage.
    $tailcount,                  // Total tail count.
    (100*$tailcount/$flipcount), // Tail count percentage.
    ($time_end - $time_start)    // Total time in seconds.
);

/* End of Benchmark */