Skip to content
Snippets Groups Projects
Verified Commit 7cbfbe7b authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Initial Commit

parents
Branches master
No related tags found
No related merge requests found
# ArduinoBenchmark
Based on https://github.com/ddworken/arduinoBenchmark and https://github.com/ddworken/arduinoSieveOfEratosthenesBenchmark.
\ No newline at end of file
//#include <EEPROM.h>
//#include <avr/pgmspace.h>
#define MAX_PRIME_V1 1000
void setup(){
Serial.begin(19200);
while (!Serial) {
;
}
Serial.println("Loading tests...");
Serial.print("Max prime number (v1): ");
Serial.println(MAX_PRIME_V1);
Serial.println("Benchmarking...");
//int eepromReadSpeed = testEepromRead();
//dot(1);
//int eepromWriteSpeed = testEepromWrite();
//dot(1);
Serial.print("integerAdditionSubtractionSpeed");
int integerAdditionSubtractionSpeed = testIntegerAdditionSubtraction();
dot(1);
Serial.print("floatingPointMultiplicationSpeed");
int floatingPointMultiplicationSpeed = testFloatingPointMultiplication();
dot(1);
Serial.print("floatingPointDivisionSpeed");
int floatingPointDivisionSpeed = testFloatingPointDivision();
dot(1);
Serial.print("integerMultiplicationSpeed");
int integerMultiplicationSpeed = testIntegerMultiplication();
dot(1);
Serial.print("integerDivisionSpeed");
int integerDivisionSpeed = testIntegerDivision();
dot(1);
Serial.print("floatingPointAdditionSubtractionSpeed");
int floatingPointAdditionSubtractionSpeed = testFloatingPointAdditionSubtraction();
dot(1);
Serial.print("analogReadSpeed");
int analogReadSpeed = testAnalogRead();
dot(1);
Serial.print("primeSieveSpeed");
int primeSieveSpeed = testPrimeSievev1();
dot(1);
Serial.println(" ");
//output(eepromReadSpeed, "EEPROM read");
//output(eepromWriteSpeed, "EEPROM write");
output(integerAdditionSubtractionSpeed, "integer addition subtraction");
output(integerMultiplicationSpeed, "integer multiplication");
output(integerDivisionSpeed, "integer division");
output(floatingPointAdditionSubtractionSpeed, "floating point addition subtraction");
output(floatingPointMultiplicationSpeed, "floating point multiplication");
output(floatingPointDivisionSpeed, "floating point division");
output(analogReadSpeed, "analog read");
output(primeSieveSpeed, "prime calculation");
Serial.println("Done.");
}
void loop(){}
/*
int testEepromWrite(){
for (int i = 0; i < 512; i++){
EEPROM.write(i, 0);
}
int startVal = millis();
for(int i = 0; i < 512; i++){
EEPROM.write(i, 1);
}
int endVal = millis();
return endVal-startVal;
}
int testEepromRead(){
int val;
int startVal = millis();
for(int i = 0; i < 2000; i++){
for(int i = 0; i < 512; i++){
val = EEPROM.read(i);
}
}
int endVal = millis();
return endVal-startVal;
}
*/
int testIntegerAdditionSubtraction(){
int volatile val;
int i;
int k;
int startVal = millis();
for(int j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val = i + 42;
}
for(k = 0; k < 10000; k++){
val = k - 42;
}
val = 0;
}
int endVal = millis();
return endVal-startVal;
}
int testIntegerMultiplication(){
int volatile val;
int i;
int k;
int startVal = millis();
for(int j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val = i *42;
}
for(k = 0; k < 10000; k++){
val = k * 42;
}
val = 0;
}
int endVal = millis();
return endVal-startVal;
}
int testIntegerDivision(){
int volatile val;
int i;
int k;
int startVal = millis();
for(int j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val = i / 42;
}
for(k = 0; k < 10000; k++){
val = k / 42;
}
val = 0;
}
int endVal = millis();
return endVal-startVal;
}
int testFloatingPointAdditionSubtraction(){
float volatile val = 42.4242;
int j;
int i;
int startVal = millis();
for(j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val=val+i;
val=42.4242;
}
for(int k = 0; k < 10000; k++){
val=val-k;
val=42.4242;
}
}
int endVal = millis();
return endVal-startVal;
}
int testFloatingPointMultiplication(){
float volatile val = 42.4242;
int j;
int i;
int startVal = millis();
for(j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val=val*i;
val=val*i;
val=42.4242;
}
}
int endVal = millis();
return endVal-startVal;
}
int testFloatingPointDivision(){
float volatile val = 42.4242;
int j;
int i;
int startVal = millis();
for(j = 0; j < 10; j++){
for(i = 0; i < 10000; i++){
val=i/val;
val=i/val;
val=42.4242;
}
}
int endVal = millis();
return endVal-startVal;
}
int testAnalogRead(){
double volatile val;
int startVal = millis();
for(int i = 0; i < 5000; i++){
val=analogRead(1);
val=analogRead(1);
}
int endVal = millis();
return endVal-startVal;
}
int testPrimeSievev1() {
int startTime = micros(); //record the current system time in the long variable time (for benchmarking purposes)
int maxNumber = MAX_PRIME_V1; //takes an input from the user and stores it in the maxNumber variable, this variable is the maximum number that this program will compute primes up to
//boolean[] booleanIsItPrimeArray = new boolean[maxNumber + 1]; //the IsItPrimeArray is used to store whether a specific number is prime
boolean booleanIsItPrimeArray[maxNumber + 1];
for (int i = 2; i <= maxNumber; i++) { //starts at 2 and sets all of the values in the array to true
booleanIsItPrimeArray[i] = true;
}
for (int i = 2; i <= sqrt(maxNumber); i++) { // i serves as what you are testing (first 2, then 3, then 5, then 7...)
if(booleanIsItPrimeArray[i] == true) {
for (int j = i; i*j <= maxNumber; j++) { //j is set equal to i (set to 2, then 3, then 5, then 7) and while i*j is less than the max number it continues
/*
* Explanation of the above two lines of code:
* -the first line serves as what multiple it is crossing off (first 2, then 3, then 5, then 7...)
* -the second line will be explained later
* -the third line serves to have it actually do the multiplication so to speak
* -it first checks if i (the multiple) times j (what we're multiplying it by) is less than or equal to maxNumber, if it is than it continues
* -it then crosses out that number by setting that location in the booleanIsItPrimeArray to false
* -once the second line loop finishes, then it goes to the above for loop and moves onto the next multiple, 3
* -if that multiple has already been set to false, (e.g. 4 would already be set to false because it is a multiple of 2), then it does not continue
* -thus if it is already false then it will loop back and try the next multiple
* -it will then continue this behavior until it reaches maxNumber, at which point it has crossed out all of the non-primes
*/
booleanIsItPrimeArray[i*j] = false; //false means it is not a prime (effectively crossing it out)
//Serial.println("Debug: setting to false"); //for debugging purposes
}
}
}
//Serial.println("1"); //prints out 1 (since this method does not account for 1 I just manually print it out)
int prime = 0; //used in calculating how many primes I calculated
for(int i=2; i<maxNumber; i++){ //starts at 2 and counts up to max number
if(booleanIsItPrimeArray[i] == true) { //if that spot is not 0, then continue
//Serial.println(i); //prints out the prime number
prime++;//increases the prime variable by 1
}
}
int endTime = micros();
//used for benchmarking purposes
return endTime - startTime;
}
void dot(int j){
for(int i = 0; i < (j - 1); i++){
Serial.print(".");
}
Serial.println(".");
}
void output(int score, String nameForTest){
Serial.print(F("You scored a "));
Serial.print(score);
Serial.print(F(" on the "));
Serial.print(nameForTest);
Serial.println(F(" test (lower is better)"));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment