After receiving my Due, I wanted to benchmark its numeric processing power as compared to the Mega. I was also interested in understanding which of my standard Arduino shields and gadgets were compatible with the 3.3v I/O of the Due.
Being a recovering physicist, what better test than to approximate pi, and periodically display the approximation results on 1638 display!
I utilized the slowly-converging Newton Approximation for pi, which does a reasonably good job of calculating pi / 4, using the infinite series 1 - 1/3 + 1/5- 1/7 + 1/9 - 1/11 +...
This is simple to express in Arduino C, and uses the floating point libraries to further assess performance. I tested a Due and a Mega, connected to a JY-LKM1638 V1.2 display module (which is based on the TM1638 controller chip). The times required to traverse 100,000 iterations were as follows:
Due: 1785 ms
Mega: 6249 ms
(roughly 3.5x performance difference)
Upping ITERATIONS to 10000000 (ten million) gets pi accurate to about 8 significant digits!
Photo attached, and sketch follows:
//
// Pi_2
//
// Steve Curd
// December 2012
//
// This program approximates pi utilizing the Newton's approximation. It quickly
// converges on the first 5-6 digits of precision, but converges verrrry slowly
// after that. For example, it takes over a million iterations to get to 7-8
// significant digits.
//
// For demonstration purposes, drives a JY-LKM1638 display module to show the
// approximated value after each 1,000 iterations, and toggles the pin13 LED for a
// visual "sign of life".
//
// I wrote this to evaluate the performance difference between the 8-bit Arduino Mega,
// and the 32-bit Arduino Due.
//
// Benchmark results for 100,000 iterations (pi accurate to 5 significant digits):
//
// Due: 1785 ms
// Mega: 6249 ms
//
// 1638 display module connections:
// VCC -> 3.3v
// GND -> GND
// DIO -> Pin 8
// CLK -> Pin 9
// STB0 -> Pin 7
//
//
#define ITERATIONS 20000000L // number of iterations
#define FLASH 1000 // blink LED every 1000 iterations
#include <TM1638.h>
// TM1638 module(DIO, CLK, STB0)
TM1638 module(8, 9, 7);
void setup() {
pinMode(13, OUTPUT);
Serial.begin(57600);
}
void loop() {
unsigned long start, time;
unsigned long niter=ITERATIONS;
int LEDcounter = 0;
boolean alternate = false;
unsigned long i, count=0; /* # of points in the 1st quadrant of unit circle */
double x = 1.0;
double temp, pi=1.0;
start = millis();
Serial.print("Beginning ");
Serial.print(niter);
Serial.println(" iterations...");
Serial.println();
count=0;
for ( i = 2; i < niter; i++) {
x *= -1.0;
pi += x / (2.0*(double)i-1);
if (LEDcounter++ > FLASH) {
LEDcounter = 0;
if (alternate) {
digitalWrite(13, HIGH);
alternate = false;
} else {
digitalWrite(13, LOW);
alternate = true;
}
temp = 40000000.0 * pi;
module.setDisplayToDecNumber( temp, 0x80);
}
}
time = millis() - start;
pi = pi * 4.0;
Serial.print("# of trials = ");
Serial.println(niter);
Serial.print("Estimate of pi = ");
Serial.println(pi, 10);
Serial.print("Time: "); Serial.print(time); Serial.println(" ms");
delay(10000);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.