- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一些函数添加类型,即速度和清晰度。我对此有几个问题。我在这里问他们所有人,因为我看到其他帖子这样做了。问题以粗体显示,以便于识别。
有一个函数我想添加类型来计算向量中负值的数量。我想让它与 Int 和 Float 兼容,就像无类型函数一样。虽然方便,但 Chris Rackauckas 建议不要这样做,请参阅 How to declare array type that can have int and floats 的第一条评论,
Do you want Ints and Floats at the same time? That's not advised due to performance reasons. But if you want to, you can use Union{Int64,Float64} as the type.
Union
向参数添加类型。 .但是,对于这种特殊情况,使用
Number
和
Real
是
Int64
的父类(super class)型和
Float64
,我认为这些会比无类型版本稍快。
Any
, 和两个类型
Number
和
Real
,
Float64
的两个父类(super class)型和
Int64
.
notypes
median time: 1.006 ms (0.00% GC)
mean time: 1.152 ms (12.60% GC)
any
median time: 1.054 ms (0.00% GC)
mean time: 1.197 ms (12.05% GC)
union
median time: 1.062 ms (0.00% GC)
mean time: 1.210 ms (12.09% GC)
number
median time: 1.110 ms (0.00% GC)
mean time: 1.315 ms (14.18% GC)
real
median time: 1.015 ms (0.00% GC)
mean time: 1.168 ms (12.70% GC)
float
median time: 728.131 μs (0.00% GC)
mean time: 829.031 μs (11.53% GC)
显然,无类型函数与类型为
Any
的函数之间没有显着改进。和
Number
.
Real
功能似乎有非常细微的改进,而
Float64
功能是唯一有明显改进的。
Int
键入的函数和 Float
通常比没有类型的更快?
Union{Int64,Float64}
是只有两种类型的父类(super class)型,不像
Number
和
Real
跨越更多。这就是为什么我认为
Union
函数会比其他函数更快(除了
Float
版本)。
是 Union
s 和无类型一样慢?
Number
)的函数是否有任何性能改进和 Float
, 与让它们保持无类型相比?
Int64
和
Float64
.
abstract type IF64 <: Union{Int64,Float64} end
但出现错误
invalid subtyping in definition of IF64
另外,
N
是什么?在函数的定义中是什么意思? (例如
where {T<:Union{Int64,Float64},N}
中的
countMPvec_arrUnion
定义如下)。
Float
函数比其他函数占用更少的内存? (
609.06 KiB
v.
921.56 KiB
)(这是基准测试的输出;代码如下)
all other functions
memory estimate: 921.56 KiB
allocs estimate: 38979
float
memory estimate: 609.06 KiB
allocs estimate: 28979
测试的完整代码:
#= -------------------
The different versions of the function
-------------------
=#
function countMPvec_notypes(vec)
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
function countMPvec_any(vec :: Any)
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
# Works with Int and Float !!!
function countMPvec_arrUnion(vec :: Array{T,1}) where {T<:Union{Int64,Float64},N}
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
# Works with Int and Float !!!
function countMPvec_arrNumber(vec :: Array{T,1}) where {T<:Number,N}
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
# Works with Int and Float !!!
function countMPvec_arrReal(vec :: Array{T,1}) where {T<:Real,N}
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
function countMPvec_arrFloat(vec :: Array{Float64,1})
nneg = 0
lvec = length(vec)
for i in 1:lvec
if (vec[i] < 0)
nneg += 1
end
end
npos = lvec - nneg
return (nneg,npos)
end
#= -------------------
Functions for benchmark
-------------------
=#
nums = [1.3; -2; 5]
nitertest = 10000
function test_notypes()
for i in 1:nitertest
countMPvec_notypes(nums)
end
end
function test_any()
for i in 1:nitertest
countMPvec_any(nums)
end
end
function test_arrUnion()
for i in 1:nitertest
countMPvec_arrUnion(nums)
end
end
function test_arrNumber()
for i in 1:nitertest
countMPvec_arrNumber(nums)
end
end
function test_arrReal()
for i in 1:nitertest
countMPvec_arrReal(nums)
end
end
function test_arrFloat()
for i in 1:nitertest
countMPvec_arrFloat(nums)
end
end
然后是基准单元。每一个都应该单独运行。此外,最后给出了每个单元格的完整输出:
import BenchmarkTools
println("notypes")
@BenchmarkTools.benchmark test_notypes()
println("any")
@BenchmarkTools.benchmark test_any()
println("union")
@BenchmarkTools.benchmark test_arrUnion()
println("number")
@BenchmarkTools.benchmark test_arrNumber()
println("real")
@BenchmarkTools.benchmark test_arrReal()
println("float")
@BenchmarkTools.benchmark test_arrFloat()
#= -------------------
Output of the benchmarks
-------------------
=#
notypes
BenchmarkTools.Trial:
memory estimate: 921.56 KiB
allocs estimate: 38979
--------------
minimum time: 855.070 μs (0.00% GC)
median time: 1.006 ms (0.00% GC)
mean time: 1.152 ms (12.60% GC)
maximum time: 31.515 ms (91.59% GC)
--------------
samples: 4314
evals/sample: 1
any
BenchmarkTools.Trial:
memory estimate: 921.56 KiB
allocs estimate: 38979
--------------
minimum time: 905.317 μs (0.00% GC)
median time: 1.054 ms (0.00% GC)
mean time: 1.197 ms (12.05% GC)
maximum time: 30.355 ms (96.33% GC)
--------------
samples: 4152
evals/sample: 1
union
BenchmarkTools.Trial:
memory estimate: 921.56 KiB
allocs estimate: 38979
--------------
minimum time: 914.563 μs (0.00% GC)
median time: 1.062 ms (0.00% GC)
mean time: 1.210 ms (12.09% GC)
maximum time: 32.472 ms (90.09% GC)
--------------
samples: 4111
evals/sample: 1
number
BenchmarkTools.Trial:
memory estimate: 921.56 KiB
allocs estimate: 38979
--------------
minimum time: 926.189 μs (0.00% GC)
median time: 1.110 ms (0.00% GC)
mean time: 1.315 ms (14.18% GC)
maximum time: 42.545 ms (97.21% GC)
--------------
samples: 3788
evals/sample: 1
real
BenchmarkTools.Trial:
memory estimate: 921.56 KiB
allocs estimate: 38979
--------------
minimum time: 863.699 μs (0.00% GC)
median time: 1.015 ms (0.00% GC)
mean time: 1.168 ms (12.70% GC)
maximum time: 31.847 ms (96.50% GC)
--------------
samples: 4257
evals/sample: 1
float
BenchmarkTools.Trial:
memory estimate: 609.06 KiB
allocs estimate: 28979
--------------
minimum time: 625.845 μs (0.00% GC)
median time: 728.131 μs (0.00% GC)
mean time: 829.031 μs (11.53% GC)
maximum time: 30.811 ms (97.38% GC)
--------------
samples: 5989
evals/sample: 1
最佳答案
所有与性能相关的问题都有一个答案:类型注释提高了结构的性能,但通常不会提高函数的性能。您从 Chris 那里读到的帖子涉及数组(结构)的元素类型,即使它出现在函数可接受的类型方面。你应该在那个上下文中解释它。
在 Julia 中,您应该对函数参数进行类型注释以控制调度,而不是为了性能。为什么?因为编译器将为您传递给它的每个具体类型专门化每个函数,从某种意义上说,会自动生成您正在考虑手动编写的所有那些专门化。所以添加类型注解的唯一原因是控制调用哪个方法:
is_this_a_number(x::Number) = true
is_this_a_number(x) = false
结构是另一回事。您可以在
Performance tips 中阅读有关结构中类型注释的更多信息。手册的部分。
Union
s 的具体类型(如果不确定,请使用
isconcretetype
检查每个)。
what does the N mean in the function's definition
关于performance - julia:关于函数中泛型类型的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69322423/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!