朱莉娅代码优化

时间:2014-09-28 16:23:13

标签: compiler-optimization julia

我有previous question的以下代码,我需要帮助优化代码以提高速度。这是代码:

function OfdmSym()
    N = 64
    n = 1000
    symbol = ones(Complex{Float64}, n, 64)
    data   = ones(Complex{Float64}, 1, 48)
    unused = zeros(Complex{Float64}, 1, 12)
    pilot  = ones(Complex{Float64}, 1, 4)
    s      = [-1-im  -1+im  1-im  1+im]

    for i=1:n                  # generate 1000 symbols
        for j = 1:48           # generate 48 complex data symbols whose basis is s
              r = rand(1:4)    # 1, 2, 3, or 4
              data[j] = s[r]
        end
        symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
    end
end
OfdmSym()

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

首先,我用N = 100000

计时
OfdmSym()  # Warmup
for i = 1:5
    @time OfdmSym()
end

并且非常快,因为它是

elapsed time: 3.235866305 seconds (1278393328 bytes allocated, 15.18% gc time)
elapsed time: 3.147812323 seconds (1278393328 bytes allocated, 14.89% gc time)
elapsed time: 3.144739194 seconds (1278393328 bytes allocated, 14.68% gc time)
elapsed time: 3.118775273 seconds (1278393328 bytes allocated, 14.79% gc time)
elapsed time: 3.137765971 seconds (1278393328 bytes allocated, 14.85% gc time)

但是我用for循环重写了以避免切片:

function OfdmSym2()
    N = 64
    n = 100000
    symbol = zeros(Complex{Float64}, n, 64)
    s      = [-1-im, -1+im, 1-im, 1+im]
    for i=1:n
        for j = 1:48
            @inbounds symbol[i,j] = s[rand(1:4)]
        end
        symbol[i,11] = one(Complex{Float64})
        symbol[i,22] = one(Complex{Float64})
        symbol[i,33] = one(Complex{Float64})
        symbol[i,44] = one(Complex{Float64})
    end
end

OfdmSym2()  # Warmup
for i = 1:5
    @time OfdmSym2()
end

快20倍

elapsed time: 0.159715932 seconds (102400256 bytes allocated, 12.80% gc time)
elapsed time: 0.159113184 seconds (102400256 bytes allocated, 14.75% gc time)
elapsed time: 0.158200345 seconds (102400256 bytes allocated, 14.82% gc time)
elapsed time: 0.158469032 seconds (102400256 bytes allocated, 15.00% gc time)
elapsed time: 0.157919113 seconds (102400256 bytes allocated, 14.86% gc time)

如果您查看分析器(@profile),您会发现大部分时间都花在生成随机数上,正如您所期望的那样,因为其他一切只是在移动数字。

答案 1 :(得分:0)

这一切都只是一点点,对吗?这根本不干净(根本没有),但它在我的机器上运行得稍微快一点(这比你的慢得多,所以我不打扰发布我的时间)。你的机器上的速度有点快吗?

function my_OfdmSym()
    const n = 100000
    const my_one_bits = uint64(1023) << 52
    const my_sign_bit = uint64(1) << 63
    my_sym = Array(Uint64,n<<1,64)
    fill!(my_sym, my_one_bits)
    for col = [1:10, 12:21, 23:32, 34:43, 45:52]
        for row = 1:(n<<1)
            if randbool() my_sym[row, col] |= my_sign_bit end
        end
    end
    my_symbol = reinterpret(Complex{Float64}, my_sym, (n, 64))
    for k in [11, 22, 33, 44]
        my_symbol[:, k] = 1.0
    end
    for k=53:64
        my_symbol[:, k] = 0.0
    end
end