versioninfo()
Julia Version 1.10.2 Commit bd47eca2c8a (2024-03-01 10:14 UTC) Build Info: Official https://julialang.org/ release Platform Info: OS: Linux (x86_64-linux-gnu) CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-15.0.7 (ORCJIT, skylake) Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
AbstractTime
¶abstract type AbstractTime end
function gethour end
function getminute end
function getsecond end
getsecond (generic function with 0 methods)
function Base.show(io::IO, time::AbstractTime)
print(io,
string(gethour(time), pad=2),
':',
string(getminute(time), pad=2),
':',
string(getsecond(time), pad=2))
end
MyTime
¶struct MyTime <: AbstractTime
hour::Int
minute::Int
second::Int
end
gethour(time::MyTime) = time.hour
getminute(time::MyTime) = time.minute
getsecond(time::MyTime) = time.second
getsecond (generic function with 1 method)
mytime1 = MyTime(14, 28, 57)
14:28:57
println(mytime1)
14:28:57
string(mytime1)
"14:28:57"
MyTime2
¶struct MyTime2 <: AbstractTime
seconds::Int
end
gethour(time::MyTime2) = time.seconds ÷ 3600
getminute(time::MyTime2) = time.seconds ÷ 60 % 60
getsecond(time::MyTime2) = time.seconds % 60
getseconds(time::MyTime2) = time.seconds
getseconds (generic function with 1 method)
function Base.show(io::IO, time::MyTime2)
@invoke show(io, time::AbstractTime) # `invoke(show, Tuple{IO, AbstractTime}, io, time)` と同じ
print(io, " (", getseconds(time), "sec(s).)")
end
mytime2 = MyTime2(10000)
02:46:40 (10000sec(s).)
string(mytime2)
"02:46:40 (10000sec(s).)"
@invoke fn(arg::AnSupertype)
のように書くと、fn(arg)
を呼ぶときに引数の型が AnSupertype
で定義されたメソッドを選択・実行(=invoke)できる!Base.@invoke ~
と書く必要ありinvoke(fn, Tuple{AnSupertype}, arg)
のように書く必要ありMyTimeWithMS
¶struct MyTimeWithMS <: AbstractTime
hms::MyTime
ms::Int
MyTimeWithMS(h, m, s, ms) = new(MyTime(h, m, s), ms)
end
gethour(time::MyTimeWithMS) = gethour(time.hms)
getminute(time::MyTimeWithMS) = getminute(time.hms)
getsecond(time::MyTimeWithMS) = getsecond(time.hms)
getmillisecond(time::MyTimeWithMS) = time.ms
getmillisecond (generic function with 1 method)
function Base.show(io::IO, time::MyTimeWithMS)
@invoke show(io, time::AbstractTime) # `invoke(show, Tuple{IO, AbstractTime}, io, time)` と同じ
print(io, '.', string(getmillisecond(time), pad=3))
end
mytime3 = MyTimeWithMS(12, 34, 56, 789)
12:34:56.789
string(mytime3)
"12:34:56.789"
MyTime
の派生型として MyTimeWithMS
を定義できない)ので、似たようなことをしたい場合はこのように(ほぼ必然的に)委譲 を利用することになる。@invoke
を用いた実装を示したが、以下のように書いても良い:function Base.show(io::IO, time::MyTimeWithMS)
print(io, time.hms, '.', string(getmillisecond(time), pad=3))
end
using BenchmarkTools
using Random
Random.seed!(1234)
@benchmark string(MyTime(h, m, s)) setup=(h=rand(0:23);m=rand(0:59);s=rand(0:59))
BenchmarkTools.Trial: 10000 samples with 365 evaluations. Range (min … max): 254.304 ns … 7.042 μs ┊ GC (min … max): 0.00% … 94.36% Time (median): 260.433 ns ┊ GC (median): 0.00% Time (mean ± σ): 277.859 ns ± 211.581 ns ┊ GC (mean ± σ): 4.17% ± 5.27% ▃█▇▅▃ ▁ ██████▆▃▄▃▂▃▅▅▆▄▅▃▄▅▄▃▃▃▂▃▅▆▅▆▆▆▇▇▆▆▆▇▇▆▃▅▅▄▂▄▂▃▄▃▃▃▃▃▃▃▄▅▃▃▄ █ 254 ns Histogram: log(frequency) by time 439 ns < Memory estimate: 424 bytes, allocs estimate: 9.
Random.seed!(1234)
@benchmark string(MyTime2(secs)) setup=(secs=rand(0:60*60*24-1))
BenchmarkTools.Trial: 10000 samples with 199 evaluations. Range (min … max): 428.714 ns … 10.469 μs ┊ GC (min … max): 0.00% … 94.56% Time (median): 448.271 ns ┊ GC (median): 0.00% Time (mean ± σ): 475.761 ns ± 472.965 ns ┊ GC (mean ± σ): 4.92% ± 4.73% █ ▁▂▇▅██▇▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁ 429 ns Histogram: frequency by time 658 ns < Memory estimate: 576 bytes, allocs estimate: 12.
Random.seed!(1234)
@benchmark string(MyTimeWithMS(h, m, s, ms)) setup=(h=rand(0:23);m=rand(0:59);s=rand(0:59);ms=rand(0:999))
BenchmarkTools.Trial: 10000 samples with 210 evaluations. Range (min … max): 359.471 ns … 10.526 μs ┊ GC (min … max): 0.00% … 95.62% Time (median): 367.214 ns ┊ GC (median): 0.00% Time (mean ± σ): 400.515 ns ± 484.719 ns ┊ GC (mean ± σ): 6.22% ± 4.95% ▆█▆▁ ▁ █████▆▄▄▃▄▄▄▅▇▇▆▅▅▆▅▅▄▁▃▁▅▆▇▆▆▇▆▆▅▅▅▅▃▆▆▆▅▆▅▅▄▃▃▅▅▅▄▅▄▅▄▅▅▅▄▅ █ 359 ns Histogram: log(frequency) by time 678 ns < Memory estimate: 608 bytes, allocs estimate: 13.