-
-
Save alpha-ninja/befa075e2ae81810100f to your computer and use it in GitHub Desktop.
testing arduino arithmetic speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define ARITHMETIC_TEST_REPETITIONS 100000 | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.println("Starting test..."); | |
randomSeed(analogRead(0)); | |
} | |
unsigned long i; | |
long microsBefore; | |
long microsTotal; | |
byte tempB; | |
unsigned int tempI; | |
unsigned long tempL; | |
int a = 4; | |
void loop() { | |
tempB = random(2, 5); | |
Serial.println("----------------- BYTE"); | |
// put your main code here, to run repeatedly: | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB + tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 2x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB + tempB + tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 3x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB + tempB + tempB + tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 4x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * 2; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x2: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * 3; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x3: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * 4; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x4: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("square: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * tempB * tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("cube: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempB = tempB * tempB * tempB * tempB; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("hypercube: "); | |
Serial.println(microsTotal); | |
tempI = random(2, 5); | |
Serial.println("----------------- UINT"); | |
// put your main code here, to run repeatedly: | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI + tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 2x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI + tempI + tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 3x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI + tempI + tempI + tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 4x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * 2; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x2: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * 3; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x3: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * 4; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x4: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("square: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * tempI * tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("cube: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempI = tempI * tempI * tempI * tempI; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("hypercube: "); | |
Serial.println(microsTotal); | |
tempL = random(2, 5); | |
Serial.println("----------------- ULONG"); | |
// put your main code here, to run repeatedly: | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL + tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 2x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL + tempL + tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 3x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL + tempL + tempL + tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("add 4x: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * 2; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x2: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * 3; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x3: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * 4; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("multiply x4: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("square: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * tempL * tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("cube: "); | |
Serial.println(microsTotal); | |
microsBefore = micros(); | |
for (i = 0; i < ARITHMETIC_TEST_REPETITIONS; i++) { | |
tempL = tempL * tempL * tempL * tempL; | |
} | |
microsTotal = micros() - microsBefore; | |
Serial.print("hypercube: "); | |
Serial.println(microsTotal); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment