我有一个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()我很感谢你的帮助。
发布于 2014-09-29 01:00:33
首先,我用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),您将看到大部分时间都花在生成随机数上,正如您所预期的那样,因为其他一切都只是在移动数字。
发布于 2014-10-01 18:19:43
都是些比特,对吧?这并不干净(完全),但它在我的机器上运行得稍微快一些(它比你的慢得多,所以我不会费心发布我的时间)。在你的机器上它是不是更快一点?
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
endhttps://stackoverflow.com/questions/26086992
复制相似问题