Hello,
I got my new DUE and wanted to do some speed tests.
I was looking for primes with my Arduinos.
All boards are tested with the same code.
The time I got from millis ().
These are the results:
from 0 to ... | UNO R3 (AVR) | Mega 2560 (AVR) | Due (ARM) |
---|---|---|---|
100 | 00:00:00.044 | 00:00:00.044 | <00:00:00.000 |
1000 | 00:00:02.968 | 00:00:02.977 | 00:00:00.014 |
10000 | 00:03:39.441 | 00:03:40.180 | 00:00:01.093 |
100000 | 04:48:12.416 | 04:51:05.177 | 00:01:23.571 |
1000000 | too long | too long | 01:56:23.084 |
The Uno and Mega are minimal different because the internal counters are not equal clocked. (capacitor inaccuracy).
In my view the DUE is much better when dealing with calculations.
But since there are only a few libraries for him, he's probably still quite a while my second choice.
My test Code:
//#########################################################################################
byte initLED = 5; //show the "boot" ##
byte runLED = 6; //show the "main" runing ##
byte finishLED = 7; //show the program is finish ##
byte speaker = 8; //the speaker pin ##
byte silentSpInterrupt = 0; //the interrupt number for the silenc button ##
boolean speakerSilent = true; //true: speaker is silent; false: speaker sounds ##
//#########################################################################################
const unsigned long n = 100; //Suchfeld = [0; n] ##
const byte w = 10; //wordwrap after "w" primes ##
const boolean printSerial = false; // true: print Prims; false: don't print Prims ##
//#########################################################################################
unsigned long amountOfPrim; //How much Prims in the Intervall I = [0; n]
unsigned long beginTime; //begin time in ms
unsigned long endTime; //end time ion ms
boolean finish = true; //give the OK for the speaker
void setup() {
pinMode(initLED, OUTPUT);
pinMode(runLED, OUTPUT);
pinMode(finishLED, OUTPUT);
pinMode(speaker, OUTPUT);
digitalWrite(initLED, HIGH);
Serial.begin(115200); //Initializing Serial
Serial.println("");
Serial.println("#########################################################");
Serial.print("Range of numbers: I = [0; ");
Serial.print(n);
Serial.println("]");
if(printSerial)
Serial.println("The results are shown (slowly)");
else
Serial.println("The results aren'n shown (faster)");
Serial.println();
Serial.println("Start :)");
Serial.println();
digitalWrite(initLED, LOW);
digitalWrite(runLED, HIGH);
beginTime = millis();
amountOfPrim = findPrim(w, n, printSerial);
endTime = millis();
Serial.println();
unsigned long needTime= endTime-beginTime; //needed Time in millis
Serial.print("Time needed:\r\n");
Serial.print(needTime);
Serial.println(" ms");
if (needTime < 36000000)
Serial.print("0");
Serial.print(needTime / 3600000);
needTime = needTime % 3600000;
Serial.print(":");
if (needTime < 600000);
Serial.print("0");
Serial.print(needTime / 60000);
needTime = needTime % 60000;
Serial.print(":");
if (needTime < 10000)
Serial.print("0");
Serial.print(needTime / 1000);
needTime = needTime % 1000;
Serial.print(".");
if (needTime < 100)
Serial.print("0");
if (needTime < 10)
Serial.print("0");
Serial.println(needTime);
Serial.println();
Serial.print("I = [0; ");
Serial.print("n");
Serial.print("] includes -> ");
Serial.print(amountOfPrim);
Serial.print(" <- primes.");
digitalWrite(runLED, LOW);
digitalWrite(finishLED, HIGH);
}
void loop(){/*
while(finish && !speakerSilent){
tone(speaker, 349);
delay(500);
noTone(speaker);
delay(100);
}*/
}
unsigned long findPrim(const unsigned long zeilenumbruch,const unsigned long numberRange, const boolean printSer){
unsigned long numberOfPrim = 0;
unsigned long toProfe = 1;
byte tempZeilenumbruch = 0;
boolean isPrim = true;
for (int i=0; i < numberRange; i++){
isPrim = true; //reste isPrim
toProfe++; //toProfe inkrementieren -> startet bei 1
for (int j = 2; j < toProfe && isPrim; j++){
if (toProfe % j == 0){
isPrim = false;
}
}
if(isPrim){
if (printSer){
Serial.print(toProfe); //Gibt Primzahl aus
Serial.print("; ");
tempZeilenumbruch++;
if( tempZeilenumbruch >= zeilenumbruch){ //Zeilenumbruch nach der "zeilenumbruch"sten Zahl
Serial.println();
tempZeilenumbruch = 0;
}
}
numberOfPrim++;
}
}
Serial.println();
return numberOfPrim;
}