Created
May 19, 2013 12:07
-
-
Save gjb2048/5607472 to your computer and use it in GitHub Desktop.
For "The difference between loops and recursion in Java and C on a Raspberry Pi" -> https://www.youtube.com/watch?v=TU7P5mqbo5Q
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
/* Recursion vs loops example. | |
(c) G J Barnard May 2013 | |
License: | |
http://creativecommons.org/licenses/by-nc-nd/3.0/ | |
*/ | |
#include <stdio.h> | |
#include <sys/time.h> | |
unsigned char aNumber = 42; | |
void ALoop(unsigned char number) | |
{ | |
while (number <= aNumber) | |
{ | |
printf("."); | |
number++; | |
} | |
printf("done\n"); | |
} | |
void ARecursion(unsigned char number) | |
{ | |
printf("."); | |
number++; | |
if (number <= aNumber) | |
{ | |
ARecursion(number); | |
} | |
else | |
{ | |
printf("done\n"); | |
} | |
} | |
int main() | |
{ | |
struct timeval start, end; | |
unsigned long duration = 0; | |
printf("Loop method:\n"); | |
gettimeofday(&start, NULL); | |
ALoop(0); | |
gettimeofday(&end, NULL); | |
duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec; | |
printf("Took: %lu micro seconds.\n", duration); | |
printf("Recursion method:\n"); | |
gettimeofday(&start, NULL); | |
ARecursion(0); | |
gettimeofday(&end, NULL); | |
duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec; | |
printf("Took: %lu micro seconds.\n", duration); | |
} |
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
// Recursion vs loops example. | |
// (c) G J Barnard May 2013 | |
// License: | |
// http://creativecommons.org/licenses/by-nc-nd/3.0/ | |
public class Recursion | |
{ | |
private byte aNumber = 42; | |
public static void main(String args[]) | |
{ | |
Recursion us = new Recursion(); | |
long currentTime; | |
System.out.println("Loop method:"); | |
currentTime = System.nanoTime(); | |
us.ALoop((byte)0); | |
System.out.println("Took: " + ((System.nanoTime() - currentTime) / 1000) + " micro seconds."); | |
System.out.println("Recursion method:"); | |
currentTime = System.nanoTime(); | |
us.ARecursion((byte)0); | |
System.out.println("Took: " + ((System.nanoTime() - currentTime) / 1000) + " micro seconds."); | |
} | |
public void ALoop(byte number) | |
{ | |
while (number <= aNumber) | |
{ | |
System.out.print("."); | |
number++; | |
} | |
System.out.println("done"); | |
} | |
public void ARecursion(byte number) | |
{ | |
System.out.print("."); | |
number++; | |
if (number <= aNumber) | |
{ | |
ARecursion(number); | |
} | |
else | |
{ | |
System.out.println("done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment