【发布时间】:2022-01-21 09:01:23
【问题描述】:
我正在用 julia 做一些实验,因为我听说它适用于科学微积分,而且它的语法让人想起 python。我试图编写并执行一个程序来计算低于某个 n 的素数,但性能并不理想。 在这里,我发布了我的代码,其中包含我昨天在 julia 编程中真正开始的免责声明,我几乎可以肯定有些地方出了问题:
n = 250000
counter = 0
function countPrime(counter)
for i = 1:n
# print("begin counter= ", counter, "\n")
isPrime = true
# print("i= ", i, "\n")
for j = 2:(i-1)
if (i%j) == 0
isPrime = false
# print("j= ", j, "\n")
break
end
end
(isPrime==true) ? counter += 1 : counter
# print("Counter= ", counter, "\n")
end
return counter
end
println(countPrime(counter))
事实上,移植到 C 中的同一个程序大约需要 5 秒的执行时间,而在 julia 中的这个程序大约需要 3 分 50 秒,这听起来很奇怪,因为我认为 julia 是一种编译语言。发生了什么事?
【问题讨论】:
标签: performance julia execution-time