- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 C++ 类(class)中,我学到了避免重复计算、使用更多加法而不是更多乘法、避免幂等技巧来提高性能。然而,当我尝试让他们用 Julia-Lang 优化代码时,我对相反的结果感到惊讶。
例如,这里有几个没有数学优化的方程式(所有代码都是用 Julia 1.1 编写的,而不是 JuliaPro):
function OriginalFunction( a,b,c,d,E )
# Oprations' count:
# sqrt: 4
# ^: 14
# * : 14
# / : 10
# +: 20
# -: 6
# = : 0+4
x1 = (1/(1+c^2))*(-c*d+a+c*b-sqrt(E))
y1 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)-(c*sqrt(E))/(1+c^2)
x2 = (1/(1+c^2))*(-c*d+a+c*b+sqrt(E))
y2 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)+(c*sqrt(E))/(1+c^2)
return [ [x1;y1] [x2;y2] ]
end
我用一些技巧优化了它们,包括:
(a*b + a*c) -> a*(b+c)
因为加法比乘法快。a^2 -> a*a
避免电源操作。x = a * (1+c^2); y = b * (1+c^2)
->
temp = 1+c^2
x = a * temp; y = b * temp
1/x -> 1.0/x
结果给出了具有更少操作的等价方程:
function SimplifiedFunction( a,b,c,d,E )
# Oprations' count:
# sqrt: 1
# ^: 0
# *: 9
# /: 1
# +: 4
# -: 6
# = : 5+4
temp1 = sqrt(E)
temp2 = c*(b - d) + a
temp3 = 1.0/(1.0+c*c)
temp4 = d - (c*(c*(d - b) - a))*temp3
temp5 = (c*temp1)*temp3
x1 = temp3*(temp2-temp1)
y1 = temp4-temp5
x2 = temp3*(temp2+temp1)
y2 = temp4+temp5
return [ [x1;y1] [x2;y2] ]
end
然后我用下面的函数测试了它们,希望操作少得多的版本玩得更快或相同:
function Test2Functions( NumberOfTests::Real )
local num = Int(NumberOfTests)
# -- Generate random numbers
local rands = Array{Float64,2}(undef, 5,num)
for i in 1:num
rands[:,i:i] = [rand(); rand(); rand(); rand(); rand()]
end
local res1 = Array{Array{Float64,2}}(undef, num)
local res2 = Array{Array{Float64,2}}(undef, num)
# - Test OriginalFunction
@time for i in 1:num
a,b,c,d,E = rands[:,i]
res1[i] = OriginalFunction( a,b,c,d,E )
end
# - Test SimplifiedFunction
@time for i in 1:num
a,b,c,d,E = rands[:,i]
res2[i] = SimplifiedFunction( a,b,c,d,E )
end
return res1, res2
end
Test2Functions( 1e6 )
然而,事实证明这 2 个函数使用相同数量的内存分配,但简化的函数有更多的垃圾收集时间,并且运行速度慢了大约 5%:
julia> Test2Functions( 1e6 )
1.778731 seconds (7.00 M allocations: 503.540 MiB, 47.35% gc time)
1.787668 seconds (7.00 M allocations: 503.540 MiB, 50.92% gc time)
julia> Test2Functions( 1e6 )
1.969535 seconds (7.00 M allocations: 503.540 MiB, 52.05% gc time)
2.221151 seconds (7.00 M allocations: 503.540 MiB, 56.68% gc time)
julia> Test2Functions( 1e6 )
1.946441 seconds (7.00 M allocations: 503.540 MiB, 55.23% gc time)
2.099875 seconds (7.00 M allocations: 503.540 MiB, 59.33% gc time)
julia> Test2Functions( 1e6 )
1.836350 seconds (7.00 M allocations: 503.540 MiB, 53.37% gc time)
2.011242 seconds (7.00 M allocations: 503.540 MiB, 58.43% gc time)
julia> Test2Functions( 1e6 )
1.856081 seconds (7.00 M allocations: 503.540 MiB, 53.44% gc time)
2.002087 seconds (7.00 M allocations: 503.540 MiB, 58.21% gc time)
julia> Test2Functions( 1e6 )
1.833049 seconds (7.00 M allocations: 503.540 MiB, 53.55% gc time)
1.996548 seconds (7.00 M allocations: 503.540 MiB, 58.41% gc time)
julia> Test2Functions( 1e6 )
1.846894 seconds (7.00 M allocations: 503.540 MiB, 53.53% gc time)
2.053529 seconds (7.00 M allocations: 503.540 MiB, 58.30% gc time)
julia> Test2Functions( 1e6 )
1.896265 seconds (7.00 M allocations: 503.540 MiB, 54.11% gc time)
2.083253 seconds (7.00 M allocations: 503.540 MiB, 58.10% gc time)
julia> Test2Functions( 1e6 )
1.910244 seconds (7.00 M allocations: 503.540 MiB, 53.79% gc time)
2.085719 seconds (7.00 M allocations: 503.540 MiB, 58.36% gc time)
谁能告诉我为什么?即使在某些性能关键代码中,5% 的速度可能也不值得为之奋斗,但我仍然很好奇:我如何帮助 Julia 编译器生成更快的代码?
最佳答案
原因是您在第二个循环中遇到了垃圾收集(而不是在第一个循环中)。如果您在循环之前执行 GC.gc()
,您将获得更多可比较的结果:
function Test2Functions( NumberOfTests::Real )
local num = Int(NumberOfTests)
# -- Generate random numbers
local rands = Array{Float64,2}(undef, 5,num)
for i in 1:num
rands[:,i:i] = [rand(); rand(); rand(); rand(); rand()]
end
local res1 = Array{Array{Float64,2}}(undef, num)
local res2 = Array{Array{Float64,2}}(undef, num)
# - Test OriginalFunction
GC.gc()
@time for i in 1:num
a,b,c,d,E = rands[:,i]
res1[i] = OriginalFunction( a,b,c,d,E )
end
# - Test SimplifiedFunction
GC.gc()
@time for i in 1:num
a,b,c,d,E = rands[:,i]
res2[i] = SimplifiedFunction( a,b,c,d,E )
end
return res1, res2
end
# call this twice as the first time you may have precompilation issues
Test2Functions( 1e6 )
Test2Functions( 1e6 )
但是,一般来说,做基准测试最好使用 BenchmarkTools.jl 包。
julia> function OriginalFunction()
a,b,c,d,E = rand(5)
x1 = (1/(1+c^2))*(-c*d+a+c*b-sqrt(E))
y1 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)-(c*sqrt(E))/(1+c^2)
x2 = (1/(1+c^2))*(-c*d+a+c*b+sqrt(E))
y2 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)+(c*sqrt(E))/(1+c^2)
return [ [x1;y1] [x2;y2] ]
end
OriginalFunction (generic function with 2 methods)
julia>
julia> function SimplifiedFunction()
a,b,c,d,E = rand(5)
temp1 = sqrt(E)
temp2 = c*(b - d) + a
temp3 = 1.0/(1.0+c*c)
temp4 = d - (c*(c*(d - b) - a))*temp3
temp5 = (c*temp1)*temp3
x1 = temp3*(temp2-temp1)
y1 = temp4-temp5
x2 = temp3*(temp2+temp1)
y2 = temp4+temp5
return [ [x1;y1] [x2;y2] ]
end
SimplifiedFunction (generic function with 2 methods)
julia>
julia> using BenchmarkTools
julia> @btime OriginalFunction()
136.211 ns (7 allocations: 528 bytes)
2×2 Array{Float64,2}:
-0.609035 0.954271
0.724708 0.926523
julia> @btime SimplifiedFunction()
137.201 ns (7 allocations: 528 bytes)
2×2 Array{Float64,2}:
0.284514 1.58639
0.922347 0.979835
julia> @btime OriginalFunction()
137.301 ns (7 allocations: 528 bytes)
2×2 Array{Float64,2}:
-0.109814 0.895533
0.365399 1.08743
julia> @btime SimplifiedFunction()
136.429 ns (7 allocations: 528 bytes)
2×2 Array{Float64,2}:
0.516157 1.07871
0.219441 0.361133
我们看到它们具有可比的性能。一般来说,您可以期望 Julia 和 LLVM 编译器会为您完成大部分此类优化(当然不能保证总是如此,但在这种情况下似乎会发生)。
编辑
我将函数简化如下:
function OriginalFunction( a,b,c,d,E )
x1 = (1/(1+c^2))*(-c*d+a+c*b-sqrt(E))
y1 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)-(c*sqrt(E))/(1+c^2)
x2 = (1/(1+c^2))*(-c*d+a+c*b+sqrt(E))
y2 = d-(c^2*d)/(1+c^2)+(c*a)/(1+c^2)+(c^2*b)/(1+c^2)+(c*sqrt(E))/(1+c^2)
x1, y1, x2, y2
end
function SimplifiedFunction( a,b,c,d,E )
temp1 = sqrt(E)
temp2 = c*(b - d) + a
temp3 = 1.0/(1.0+c*c)
temp4 = d - (c*(c*(d - b) - a))*temp3
temp5 = (c*temp1)*temp3
x1 = temp3*(temp2-temp1)
y1 = temp4-temp5
x2 = temp3*(temp2+temp1)
y2 = temp4+temp5
x1, y1, x2, y2
end
只专注于计算的核心并对其运行@code_native
。这是它们(去掉注释以缩短它们)。
.text
pushq %rbp
movq %rsp, %rbp
subq $112, %rsp
vmovaps %xmm10, -16(%rbp)
vmovaps %xmm9, -32(%rbp)
vmovaps %xmm8, -48(%rbp)
vmovaps %xmm7, -64(%rbp)
vmovaps %xmm6, -80(%rbp)
vmovsd 56(%rbp), %xmm8 # xmm8 = mem[0],zero
vxorps %xmm4, %xmm4, %xmm4
vucomisd %xmm8, %xmm4
ja L229
vmovsd 48(%rbp), %xmm9 # xmm9 = mem[0],zero
vmulsd %xmm9, %xmm3, %xmm5
vsubsd %xmm5, %xmm1, %xmm5
vmulsd %xmm3, %xmm2, %xmm6
vaddsd %xmm5, %xmm6, %xmm10
vmulsd %xmm3, %xmm3, %xmm6
movabsq $526594656, %rax # imm = 0x1F633260
vmovsd (%rax), %xmm7 # xmm7 = mem[0],zero
vaddsd %xmm7, %xmm6, %xmm0
vdivsd %xmm0, %xmm7, %xmm7
vsqrtsd %xmm8, %xmm8, %xmm4
vsubsd %xmm4, %xmm10, %xmm5
vmulsd %xmm5, %xmm7, %xmm8
vmulsd %xmm9, %xmm6, %xmm5
vdivsd %xmm0, %xmm5, %xmm5
vsubsd %xmm5, %xmm9, %xmm5
vmulsd %xmm3, %xmm1, %xmm1
vdivsd %xmm0, %xmm1, %xmm1
vaddsd %xmm5, %xmm1, %xmm1
vmulsd %xmm2, %xmm6, %xmm2
vdivsd %xmm0, %xmm2, %xmm2
vaddsd %xmm1, %xmm2, %xmm1
vmulsd %xmm3, %xmm4, %xmm2
vdivsd %xmm0, %xmm2, %xmm0
vsubsd %xmm0, %xmm1, %xmm2
vaddsd %xmm10, %xmm4, %xmm3
vmulsd %xmm3, %xmm7, %xmm3
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm8, (%rcx)
vmovsd %xmm2, 8(%rcx)
vmovsd %xmm3, 16(%rcx)
vmovsd %xmm0, 24(%rcx)
movq %rcx, %rax
vmovaps -80(%rbp), %xmm6
vmovaps -64(%rbp), %xmm7
vmovaps -48(%rbp), %xmm8
vmovaps -32(%rbp), %xmm9
vmovaps -16(%rbp), %xmm10
addq $112, %rsp
popq %rbp
retq
L229:
movabsq $throw_complex_domainerror, %rax
movl $72381680, %ecx # imm = 0x45074F0
vmovapd %xmm8, %xmm1
callq *%rax
ud2
ud2
nop
和
.text
pushq %rbp
movq %rsp, %rbp
subq $64, %rsp
vmovaps %xmm7, -16(%rbp)
vmovaps %xmm6, -32(%rbp)
vmovsd 56(%rbp), %xmm0 # xmm0 = mem[0],zero
vxorps %xmm4, %xmm4, %xmm4
vucomisd %xmm0, %xmm4
ja L178
vmovsd 48(%rbp), %xmm4 # xmm4 = mem[0],zero
vsqrtsd %xmm0, %xmm0, %xmm0
vsubsd %xmm4, %xmm2, %xmm5
vmulsd %xmm3, %xmm5, %xmm5
vaddsd %xmm1, %xmm5, %xmm5
vmulsd %xmm3, %xmm3, %xmm6
movabsq $526593928, %rax # imm = 0x1F632F88
vmovsd (%rax), %xmm7 # xmm7 = mem[0],zero
vaddsd %xmm7, %xmm6, %xmm6
vdivsd %xmm6, %xmm7, %xmm6
vsubsd %xmm2, %xmm4, %xmm2
vmulsd %xmm3, %xmm2, %xmm2
vsubsd %xmm1, %xmm2, %xmm1
vmulsd %xmm3, %xmm1, %xmm1
vmulsd %xmm1, %xmm6, %xmm1
vsubsd %xmm1, %xmm4, %xmm1
vmulsd %xmm3, %xmm0, %xmm2
vmulsd %xmm2, %xmm6, %xmm2
vsubsd %xmm0, %xmm5, %xmm3
vmulsd %xmm3, %xmm6, %xmm3
vsubsd %xmm2, %xmm1, %xmm4
vaddsd %xmm5, %xmm0, %xmm0
vmulsd %xmm0, %xmm6, %xmm0
vaddsd %xmm1, %xmm2, %xmm1
vmovsd %xmm3, (%rcx)
vmovsd %xmm4, 8(%rcx)
vmovsd %xmm0, 16(%rcx)
vmovsd %xmm1, 24(%rcx)
movq %rcx, %rax
vmovaps -32(%rbp), %xmm6
vmovaps -16(%rbp), %xmm7
addq $64, %rsp
popq %rbp
retq
L178:
movabsq $throw_complex_domainerror, %rax
movl $72381680, %ecx # imm = 0x45074F0
vmovapd %xmm0, %xmm1
callq *%rax
ud2
ud2
nopl (%rax,%rax)
可能你不会想详细消化它,但是你可以看到简化后的函数使用的指令少了一些,但只有几条,如果你比较原始代码,这可能会令人惊讶。例如,两个代码都只调用一次 sqrt
(因此在第一个函数中对 sqrt
的多次调用得到了优化)。
关于math - 为什么简化的数学方程比在 Julia-Lang 中有更多运算的等价方程运行得(稍微)慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281933/
我正在开发一个带选项卡栏的 ios 应用程序。我的栏上有超过 5 个按钮,所以在 iphone 上我有更多的按钮。现在,假设我有这个按钮:Button1 Button2 Button3 Button4
我有一个带有 UITabBarController 的应用,其中有超过五个选项卡。 当我按更多选项卡时,我会转到moreNavigationController,它是一个UINavigationCon
我有一个导航 Controller 。 NAVC->MORE... 按钮,然后在“更多”下有一些额外的 VC。 如果我转到“更多...”下的 VC,然后转到不在“更多...”上的 VC,那么当我返回到
因此,我想出了这种方案,用于在多个线程同时具有读写访问权限的二叉树中旋转时锁定节点,这涉及每次旋转锁定四个节点,这似乎是一个很多吗?我想到了一种比我想出的方法更聪明的方法来减少所需的锁定,但谷歌并没有
所以我已经尝试了所有方法,但我似乎仍然无法将下拉内容与 dropbtn 对齐。我只希望内容始终位于更多菜单下方。 HTML: `
我正在尝试使用 expect 来自动接受在 --more-- 中提示的 EULA。 #!/usr/bin/expect spawn "./greenplum-perfmon-web-4.1.2.0-b
他们如何在下面提供的网站上制作“告诉我更多”效果。我读过 read more/less effect in jQuery,但我发现该站点的有趣之处在于,除非单击该按钮,否则无法滚动页面。 Effect
现在,Kim Stebel helped me understanding如何使用存在类型键入变量,我需要知道如何在继承中使用它们: 以下代码无法编译: class PagingListModel(s
在我的Cygwin中不可用。另一方面,提供了“ less”命令。也许Cygwin的制造商认为“更多”只是多余的。 我对此很好奇。 最佳答案 安装util-linux软件包,您将获得“更多”的信息 ht
基本上,我想知道是否有人有增加 DTU 与分片的经验。 DTU应该线性地提高性能。因此,如果您有 5 个 DTU,而您改为 10 个 DTU,那么(理论上)您应该获得大约两倍的性能。 因此,四个 SQ
我们使用 asp.net mvc、javascript 和 jQuery(托管在本地计算机上)创建了一个应用程序。基本设计是,当用户从一个页面导航到其他页面时,我们通过隐藏和显示 HTML 页面,将所
我想用 RMonad 做一些基本的事情。有没有办法使用“as monad”功能来 有一个身份 rmonad,可以应用 monad 转换器吗? 有诸如 StateT 变压器之类的常见东西吗? 向现有 m
我有一个 char*[] 数组。我需要能够为其分配字符串并再次删除它们,但我不知道: 如何检查一个元素中是否已经有一个字符串,这样我就不会覆盖它,如果它已经被占用,则继续处理下一个元素? 之后如何将其
基本上,我想知道是否有人有增加 DTU 与分片的经验。 DTU应该线性地提高性能。因此,如果您有 5 个 DTU,而您改为 10 个 DTU,那么(理论上)您应该获得大约两倍的性能。 因此,四个 SQ
我有一个程序可以同时吐出标准错误和标准输出,我想在标准错误上少运行寻呼机,但忽略标准输出。我该怎么做? 更新: 就是这样......我不想丢失标准输出......只是让它远离寻呼机 program 2
基本上,当单击具有类 "dropdown" 的链接时,我无法获取“更多...”链接来对下一个跨度的高度进行动画处理。它根本就没有动画。仅当更改为 Less... 链接并且单击 Less... 链接以折
我正在使用 ExtJS,并认为它是一个了不起的框架。但是,它们没有内置的状态图,这使得依赖于状态的应用程序开发非常痛苦。 我最近发现了这个: https://github.com/jakesgordo
我一直在研究数据结构和算法,遗憾的是在C中。我已经单独实现了一个双向链表,它保存整数并且工作正常,但是当节点(或pub)让它正常工作时我遇到了很多麻烦在本例中)保存多个不同类型的值。我可以创建一个列表
编辑拼写错误 你好, 这可能是一个愚蠢的问题,但如果它能帮助我遵循最佳实践,我不在乎:P 假设我想在 System.Data 命名空间...以及 System.Data.SqlClient 命名空间中
使用 bootstrap 3 CSS、font awesome CSS 和最新的 jQuery JS 文件。 我正在使用 javascript 在单击按钮时在另一个内容 div 之上隐藏/显示一个内容
我是一名优秀的程序员,十分优秀!