100 Million Things To Do In Under 2 Minutes

Raspberry Pi 2

Raspberry Pi 2

According to Douglas Adams, it was the ‘Long Dark Tea-Time of the Soul‘, better known to all as Sunday afternoon. You can either hate them or engage them. Your mind begins to wander. And then you land on a thought. It won’t go away. An itch that needs scratching.

For me it was wondering how many lines of python the new Raspberry Pi 2 would execute per second. Just to give me a feel for how much work the Pi can actually do. I have 6 different Raspberry Pi models and a comparison would be interesting.

I wrote some simple python code:

#!/usr/bin/env python
import time
n = 0
print " \nBefore " + str(time.time())
while n < 100000000:
    n += 1
print " \nAfter " + str(time.time())

NB. This is the final version of the code.

I wrote and tested the code on my Windows desktop before transferring it to the Pi. And the first time I ran it I had to crash out because I had forgotten to increment the counter. An endless loop! Ooops.

The code is very simple. I Just wanted a time-stamp before and after, and a minimal loop, to get a rough idea of speed. The amount of work that was being done.

I set the loop to 10,000 iterations to start with. The ran. It completed within the same time-stamp. Almost instantaneous. So I upped the loop to 100,000. Again a fraction of a second. Then a million. Again inside a second. So I jumped to 100,000,000. 100 million iterations. This took 8 seconds on the desktop.

My Windows desktop is an intel core i7 3770K. The program was only running in 2 cores. Using 25% of those cores. Roughly 8% of the processing capacity.

The code was working so I transferred it to the Pi 2.  I thought that this was going to take ages to run. First time through it took 156 seconds which I then got down to 115 seconds with the standard 1000mhz overclock. To process 300 million lines of python code. I find that amazing. (100 million iterations * 3 lines of code.) That works out to be 2,600,000 lines of python code a second. On a Pi.


(04/12/2015 Updated to include new Pi Zero.)
(04/03/2016 Updated to include new Pi 3 Model B, at just under a minute, and an original release Model B 256mb at 290 seconds.)

NB. The Pi 2 result came from running the program simultaneously in 4 cores and taking the longest core time.

This is in no way scientific. It is not a performance test. But as a very rough guide to just how powerful a Raspberry Pi can be, regardless of model, I found it amazing.

(Do try this at home!)

And links to some more thorough Raspberry Pi Benchmarking:

Pi Foundation