I’m taking the time to pick up python right now. So far I like it, very straightforward, has everything I want in a language. One of the things I do when I pick a new language is re-implement the very first program I wrote when learning how to program my calculator in High School. Its a very advanced piece of technology that “tosses coins”. Anyway, usually figuring out how to do it in a new language is a quick way to get the hang of the syntax and basics. Anyway, I did it today in php, python, c and JavaScript. Then for the heck of it I tested all three to see which was faster at flipping a coin a million times.
The results annoyed the crap out of me. I want to hate php. I want to break free of php. But it keeps pulling me back in. In a battle between php and python in the field of coin tossing, php mops the floor with python. The php script was six times faster (587ms vs 3075ms). But the python script was much more pleasant to write, for what its worth.
I wasn’t at all surprised by how well JavaScript (running in Node/V8) performed. The js script did the job in 123ms, better than 2x the php script. The C program took 40ms.
Obviously this is not a real test in any way, but the results were interesting:
> time ./toss.php
Heads: 501211
Tails: 498789
real 0m0.587s
user 0m0.555s
sys 0m0.015s
> time ./toss.py
{'tails': 500309, 'heads': 499691}
real 0m3.075s
user 0m2.989s
sys 0m0.052s
> time ./a.out
heads= 499798, tails= 500202
real 0m0.040s
user 0m0.027s
sys 0m0.002s
> time ./flip.js
{"heads":500140,"tails":499860}
real 0m0.123s
user 0m0.111s
sys 0m0.009s
Anyway, here is the code, my apologies if the C and python are ugly, I’m new to both:
PHP:
#!/usr/bin/php
<?php
function getRandom(){
return rand(0,1);
}
$num = 1000000;
$heads = 0;
$tails = 0;
for($i=0; $i < $num; $i++){
$t = getRandom();
if($t){
$heads++;
}else{
$tails++;
}
}
echo("Heads: $heads\nTails: $tails\n");
And in Python:
#!/usr/bin/python
import random
rand = random.Random()
results = {"heads":0, "tails":0}
num = 1000000
def toss():
return rand.randint(0,1)
for i in range(0, num):
if toss():
results['heads'] += 1
else:
results['tails'] += 1
print results
C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int i, num, heads, tails;
for (i=0; i<1000000; i++)
{
num = rand() %10;
if(num > 4){
tails++;
}else{
heads++;
}
}
printf ("heads= %d, tails= %u", heads, tails);
return 0;
}
Javascript:
#!/usr/local/bin/node
var sys = require("sys");
var i, len=1000000, floor=Math.floor, rand=Math.random;
function toss()
{
return floor(rand() * 2) % 2;
}
var results = {heads:0,tails:0};
for(i =0; i < len; i++ ){
if(toss()){
results.tails++;
}else{
results.heads++;
}
}
sys.puts(JSON.stringify(results));
(Isaac contributed a few performance suggestions for c and javascript. I had a bug with the c one, so I ignored it)
4 Comments
Did you try the js/node version without JSON.stringify? It’s a far more complicated operation than just printing output.
Well, technically, done once after the loop shouldn’t change the results much. For some reason (read: lack of sleep) I thought it was in the loop
You and I think alike without sleep. ;P
object Coin { val rand = new scala.util.Random() def toss() = { rand.nextBoolean() } } var heads = 0 var tails = 0 val range = 0.until(1000000) for (i <- range) { if (Coin.toss()) { heads += 1 } else { tails += 1 } } println("Heads: " + heads + " Tails: " + tails)