performance - 执行时间 julia 程序计算素数

标签 performance julia execution-time

我正在对 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 是一种编译语言。发生了什么事?

最佳答案

下面是我将如何改变它:

function countPrime(n)
    counter = 0
    for i in 1:n
        isPrime = true
        for j in 2:i-1
            if i % j == 0 
                isPrime = false
                break
            end            
        end
        isPrime && (counter += 1)
    end
    return counter
end

此代码在我的笔记本电脑上运行大约 5 秒。除了风格上的变化之外,主要的变化是您应该将 n 作为参数传递给您的函数,并在您的函数中定义 counter 变量。

更改遵循 Performance Tips 中的第一个建议。 Julia 手册的一部分。

重点是当你使用一个全局变量时,Julia 编译器无法假设这个变量的类型(因为它可能在函数编译后改变),所以它防御性地假设它可能是任何东西,这会减慢速度。

至于文体变化,请注意 (isPrime==true) ? counter += 1 : counter 可以写成 isPrime && (counter += 1) 因为如果 isPrime 你想递增计数器真。使用三元运算符? : 这里不需要。


给出在函数中使用全局变量的问题的 MWE:

julia> x = 10
10

julia> f() = x
f (generic function with 1 method)

julia> @code_warntype f()
MethodInstance for f()
  from f() in Main at REPL[2]:1
Arguments
  #self#::Core.Const(f)
Body::Any
1 ─     return Main.x

您可以看到,在 f 函数中,您引用了全局变量 x。因此,当 Julia 编译 f 时,它必须假定 x 的值可以是任何类型(在 Julia 中称为 Any)。使用此类值很慢,因为编译器无法使用任何优化来利用更具体类型的值。

关于performance - 执行时间 julia 程序计算素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70410054/

相关文章:

audio - Julia 中用于音频处理的实时 STFT 和 ISTFT

javascript - 超过最大执行时间谷歌表格脚本解决方法

c++ - 线程在给定时隙内执行的指令数是否最少?

c++ - 为什么 `std::copy` 在我的测试程序中从 char 缓冲区读取一个 int 比 `memcpy` 慢 5 倍(!)?

python - 向量化或优化循环,其中每次迭代都取决于前一次迭代的状态

c# - 如何衡量 MVC 网站的性能?

julia - 运行 Julia .jl 文件

Julia 对微分方程积分 : MethodError: no method matching

algorithm - 算法分析——三个嵌套依赖循环的时间复杂度

javascript - React Native onPress 事件延迟