Skip to content

Instantly share code, notes, and snippets.

@pluser
Created May 25, 2020 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pluser/37545c09dc59ca0e6c8e89ff2444390f to your computer and use it in GitHub Desktop.
Save pluser/37545c09dc59ca0e6c8e89ff2444390f to your computer and use it in GitHub Desktop.
compare speed of loop with Python and C.
#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;
}
#!/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