考虑下面的代码(Julia)
bar(x) = for i = 1:9999 x+x*x-x+x end # Define the "bar" function
print("First try: "); @time bar(0.5)
print("Second try: "); @time bar(0.5)
bar(x) = for i = 1:9999 x+x*x-x+x end # Redefine the same "bar" function
print("Third try: "); @time bar(0.5)
print("Fourth try: "); @time bar(0.6)
输出是
First try: elapsed time: 0.002738996 seconds (88152 bytes allocated)
Second try: elapsed time: 3.827e-6 seconds (80 bytes allocated)
Third try: elapsed time: 0.002907554 seconds (88152 bytes allocated)
Fourth try: elapsed time: 2.395e-6 seconds (80 bytes allocated)
为什么第二(第四)尝试比第一(第三)更快(占用更少的内存)?
据我了解,Julia具有即时编译器。因此,第一次(和第三次)运行是编译代码(具有所需的分配),而第二次(和第四次)运行只是运行先前编译的代码
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句