Julia开发工具与性能优化全解析
1. 文档字符串与函数定义
在Julia中,文档字符串是紧跟在函数定义前的字符串,用于提供函数的帮助信息。例如,在 panto.jl
文件中,定义了三个与哑剧角色相关的函数: aladdin
、 genie
和 alibaba
。
"""
Calculate the sum of the series i/(i+1)^2 using the genie function for
an integer i in the range [1:n].
"""
function aladdin(n::Integer)
@assert n > 0
s = 0.0
for i in 1:n
s += genie(i,2)
end
return s
end
"""
Compute the value of the expression x/(x+1)^k where x is a numeric and
k is a (non-complex) number.
"""
genie(x,k) = x/(x+1)^k
const N_THIEVES = 40;
"""
Compute and store the items using Aladdin's genie storing each partial
sum of the series i/(i+1)^2 in an array upto a value of 40.
So