Performance 与茱莉亚的表演。在布尔数据中循环

Performance 与茱莉亚的表演。在布尔数据中循环,performance,boolean,julia,boolean-operations,Performance,Boolean,Julia,Boolean Operations,这是我当前的代码(代码中比较慢的部分) 有没有办法改进这个代码?我感觉自己在a和b之间循环了两次。我尝试使用for循环和if语句,但速度慢得多 onefalse = a $ b twofalses = !a & !b sum(onefalse) sum(twofalses) 首先,一个有效的问题是为什么for循环版本很慢。部分原因是randbool给出的是位数组,而不是普通数组。出于好奇,我决定与数组{Bool}进行比较。我能把它加快一点——我想你可能已经到了极限。特别是,索引到数组{

这是我当前的代码(代码中比较慢的部分)

有没有办法改进这个代码?我感觉自己在
a
b
之间循环了两次。我尝试使用
for
循环和if语句,但速度慢得多

onefalse = a $ b
twofalses = !a & !b
sum(onefalse)
sum(twofalses)

首先,一个有效的问题是为什么for循环版本很慢。部分原因是
randbool
给出的是
位数组,而不是普通数组。出于好奇,我决定与
数组{Bool}
进行比较。我能把它加快一点——我想你可能已经到了极限。特别是,索引到
数组{Bool}
似乎比
位数组
要快,但是像这里这样对
位数组
进行的操作很难打败

for i = 1:N
        if a[i]
            if b[i]

            else
                onefalse+=1
            end
        else
            if b[i]
                onefalse+=1
            else
                twofalses+=1
            end
        end
       end
给予


首先,一个有效的问题是为什么for循环版本很慢。部分原因是
randbool
给出的是
位数组,而不是普通数组。出于好奇,我决定与
数组{Bool}
进行比较。我能把它加快一点——我想你可能已经到了极限。特别是,索引到
数组{Bool}
似乎比
位数组
要快,但是像这里这样对
位数组
进行的操作很难打败

for i = 1:N
        if a[i]
            if b[i]

            else
                onefalse+=1
            end
        else
            if b[i]
                onefalse+=1
            else
                twofalses+=1
            end
        end
       end
给予


除了IainDunning的出色回答之外,值得提醒的是:如果您关心性能,请始终将内容放在函数中。请参阅手册的性能提示页面。除了Iaindenning的出色回答之外,值得提醒的是:如果您关心性能,请始终将内容放在函数中。请参阅手册的“性能提示”页。
function countfalse1(N, a, b)
    return sum(a $ b), sum(!a & !b)
end

function countfalse2(N, a, b)
    return N-sum(a), N-sum(a|b)
end

function countfalse3(N, a, b)
    onef, twof = 0, 0
    @inbounds for i = 1:N
        if a[i]
            if !b[i]
                onef += 1
            end
        else
            if b[i]
                onef += 1
            else
                twof += 1
            end
        end
   end
   return onef, twof
end

srand(1000)
N = 10000000
a = randbool(N)
b = randbool(N)
c = Bool[a[i] for i in 1:N]
d = Bool[b[i] for i in 1:N]

println("BitArray")
@show countfalse1(N, a, b)
@time countfalse1(N, a, b)
@time countfalse1(N, a, b)
@time countfalse1(N, a, b)

@show countfalse2(N, a, b)
@time countfalse2(N, a, b)
@time countfalse2(N, a, b)
@time countfalse2(N, a, b)

@show countfalse3(N, a, b)
@time countfalse3(N, a, b)
@time countfalse3(N, a, b)
@time countfalse3(N, a, b)

println("\nArray{Bool}")
@show countfalse1(N, c, d)
@time countfalse1(N, c, d)
@time countfalse1(N, c, d)
@time countfalse1(N, c, d)

@show countfalse2(N, c, d)
@time countfalse2(N, c, d)
@time countfalse2(N, c, d)
@time countfalse2(N, c, d)

@show countfalse3(N, c, d)
@time countfalse3(N, c, d)
@time countfalse3(N, c, d)
@time countfalse3(N, c, d)
BitArray
countfalse1(N,a,b) => (5001756,2500026)
elapsed time: 0.004565573 seconds (5014328 bytes allocated)
elapsed time: 0.003607561 seconds (5000528 bytes allocated)
elapsed time: 0.013880181 seconds (5000528 bytes allocated, 83.83% gc time)
countfalse2(N,a,b) => (5003620,2500026)
elapsed time: 0.000784883 seconds (1250240 bytes allocated)
elapsed time: 0.000752576 seconds (1250240 bytes allocated)
elapsed time: 0.000758695 seconds (1250240 bytes allocated)
countfalse3(N,a,b) => (5001812,2500026)
elapsed time: 0.120491323 seconds (144 bytes allocated)
elapsed time: 0.118401949 seconds (144 bytes allocated)
elapsed time: 0.11807728 seconds (144 bytes allocated)

Array{Bool}
countfalse1(N,c,d) => (5001756,2500026)
elapsed time: 0.098838752 seconds (40000640 bytes allocated)
elapsed time: 0.112468122 seconds (40000640 bytes allocated, 10.64% gc time)
elapsed time: 0.11305269 seconds (40000640 bytes allocated, 10.22% gc time)
countfalse2(N,c,d) => (5003620,2500026)
elapsed time: 0.066169587 seconds (10000328 bytes allocated)
elapsed time: 0.084794646 seconds (10000328 bytes allocated, 17.78% gc time)
elapsed time: 0.067458965 seconds (10000328 bytes allocated)
countfalse3(N,c,d) => (5001812,2500026)
elapsed time: 0.066095076 seconds (144 bytes allocated)
elapsed time: 0.067585543 seconds (144 bytes allocated)
elapsed time: 0.06718118 seconds (144 bytes allocated)