考虑以下代码(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)
为什么第二次(和第四次)尝试比第一次(和第三次)尝试更快(并且占用更少的内存)?