6 ms·
Are labels on first output misplaced or mojo was actually slower? ’’’ 3628800 Time taken: 3.0279159545898438e-05 seconds for mojo 3628800 Time taken: 5.006790
by boguscoder 1y ago
Are labels on first output misplaced or mojo was actually slower?
’’’
3628800
Time taken: 3.0279159545898438e-05 seconds for mojo
3628800
Time taken: 5.0067901611328125e-06 seconds for python
’’’
- que-encrypt 1y agoMy guess is that the slight overhead of interacting with mojo led to this speed discrepancy, and if a higher factorial (that was within the overflow limits etc) was run, this overhead would become negligible (as seen by the second example). Also similar to jax code being slower than numpy code for small operations, but being much faster for larger ones on cpus etc.
- mkl 1y agoThe Python function is implemented in C and uses a faster algorithm [1], and this particular factorial is so small they put it in a lookup table [2]. It is a strange and very unequal choice for a demo. [1] https://github.com/python/cpython/blob/0d9d48959e050b66cb37a333940ebf4dc2a74e15/Modules/mathmodule.c#L1819 https://github.com/python/cpython/blob/0d9d48959e050b66cb37a... [2] https://github.com/python/cpython/blob/0d9d48959e050b66cb37a333940ebf4dc2a74e15/Modules/mathmodule.c#L1992 https://github.com/python/cpython/blob/0d9d48959e050b66cb37a...