Created
May 25, 2020 08:27
-
-
Save pluser/37545c09dc59ca0e6c8e89ff2444390f to your computer and use it in GitHub Desktop.
compare speed of loop with Python and C.
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
#include <stdio.h> | |
int counter = 0; | |
int tarai(int x, int y, int z) { | |
counter++; | |
if (x <= y) return y; | |
else return tarai(tarai(x-1, y, z), tarai(y-1, z, x), tarai(z-1, x, y)); | |
} | |
int main(int argc, char* argv[]) { | |
int result = tarai(12, 6, 0); | |
printf("tarai(12, 6, 0) = %d\t<n_call:%d>\n", result, counter); | |
return 0; | |
} |
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
#!/usr/bin/python | |
counter = 0 | |
def tarai(x, y, z): | |
global counter | |
counter += 1 | |
if x <= y: | |
return y | |
else: | |
return tarai(tarai(x-1,y,z), tarai(y-1,z,x), tarai(z-1,x,y)) | |
if __name__ == '__main__': | |
print("tarai(12, 6, 0) = {}\t<n_call:{}>".format(tarai(12, 6, 0), counter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment