Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active March 16, 2024 00:22
Show Gist options
  • Select an option

  • Save antimon2/24bd252590482088809dce7362aa3ce7 to your computer and use it in GitHub Desktop.

Select an option

Save antimon2/24bd252590482088809dce7362aa3ce7 to your computer and use it in GitHub Desktop.
MyTimes.py.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.841Z",
"end_time": "2024-03-16T09:17:33.497000+09:00"
},
"trusted": true
},
"id": "4a3ab2bb",
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.10.2\nCommit bd47eca2c8a (2024-03-01 10:14 UTC)\nBuild Info:\n Official https://julialang.org/ release\nPlatform Info:\n OS: Linux (x86_64-linux-gnu)\n CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-15.0.7 (ORCJIT, skylake)\nThreads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## `AbstractTime`"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.842Z",
"end_time": "2024-03-16T09:17:33.842000+09:00"
},
"trusted": true
},
"id": "7f0325e7",
"cell_type": "code",
"source": "abstract type AbstractTime end\n\nfunction gethour end\nfunction getminute end\nfunction getsecond end",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "getsecond (generic function with 0 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.843Z",
"end_time": "2024-03-16T09:17:33.850000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# ↓のデフォルト実装も先にしておく\ngetseconds(time::AbstractTime) = \n (gethour(time) * 60 + getminute(time)) * 60 + getsecond(time)\ngetmillisecond(::AbstractTime) = 0",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "getmillisecond (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Trait Definitions"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.843Z",
"end_time": "2024-03-16T09:17:33.853000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# Trait types\nabstract type TimeStyle end\n\nstruct Simple <: TimeStyle end\nstruct WithSeconds <: TimeStyle end\nstruct WithMS <: TimeStyle end",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true,
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.844Z",
"end_time": "2024-03-16T09:17:34.106000+09:00"
}
},
"cell_type": "code",
"source": "# トレイト型のデフォルト= `Simple()`\nTimeStyle(::Type{<:AbstractTime}) = Simple()",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "TimeStyle"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### `Base.show()` および `showtime()` の定義"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.845Z",
"end_time": "2024-03-16T09:17:34.107000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.show(io::IO, time::T) where {T <: AbstractTime} = showtime(io, TimeStyle(T), time)",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.846Z",
"end_time": "2024-03-16T09:17:34.120000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function showtime(io::IO, ::Simple, time)\n print(io,\n string(gethour(time), pad=2),\n ':',\n string(getminute(time), pad=2),\n ':',\n string(getsecond(time), pad=2))\nend",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "showtime (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.847Z",
"end_time": "2024-03-16T09:17:34.121000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function showtime(io::IO, ::WithSeconds, time)\n showtime(io, Simple(), time) # ←ココ!\n print(io, \" (\", getseconds(time), \"sec(s).)\")\nend",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "showtime (generic function with 2 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.848Z",
"end_time": "2024-03-16T09:17:34.122000+09:00"
},
"trusted": true
},
"id": "78828938",
"cell_type": "code",
"source": "function showtime(io::IO, ::WithMS, time)\n showtime(io, Simple(), time) # ←ココ!\n print(io, '.', string(getmillisecond(time), pad=3))\nend",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "showtime (generic function with 3 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"id": "c6d657a4",
"cell_type": "markdown",
"source": "## `MyTime`"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.848Z",
"end_time": "2024-03-16T09:17:34.124000+09:00"
},
"trusted": true
},
"id": "6d4591d3",
"cell_type": "code",
"source": "struct MyTime <: AbstractTime\n hour::Int\n minute::Int\n second::Int\nend\n\ngethour(time::MyTime) = time.hour\ngetminute(time::MyTime) = time.minute\ngetsecond(time::MyTime) = time.second",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "getsecond (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.849Z",
"end_time": "2024-03-16T09:17:34.232000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "TimeStyle(MyTime) === Simple()",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.849Z",
"end_time": "2024-03-16T09:17:34.353000+09:00"
},
"trusted": true
},
"id": "11c6575c",
"cell_type": "code",
"source": "mytime = MyTime(14, 28, 57)",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "14:28:57"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.850Z",
"end_time": "2024-03-16T09:17:34.679000+09:00"
},
"trusted": true
},
"id": "c0b80fec",
"cell_type": "code",
"source": "string(mytime)",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "\"14:28:57\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"id": "8d61369c",
"cell_type": "markdown",
"source": "## `MyTime2`"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.850Z",
"end_time": "2024-03-16T09:17:34.683000+09:00"
},
"trusted": true
},
"id": "7377046c",
"cell_type": "code",
"source": "struct MyTime2 <: AbstractTime\n seconds::Int\nend\n\ngethour(time::MyTime2) = time.seconds ÷ 3600\ngetminute(time::MyTime2) = time.seconds ÷ 60 % 60\ngetsecond(time::MyTime2) = time.seconds % 60\ngetseconds(time::MyTime2) = time.seconds # override\n\nTimeStyle(::Type{MyTime2}) = WithSeconds() # ←ココ!",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "TimeStyle"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.851Z",
"end_time": "2024-03-16T09:17:34.809000+09:00"
},
"trusted": true
},
"id": "3bf3c07f",
"cell_type": "code",
"source": "mytime2 = MyTime2(10000)",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "02:46:40 (10000sec(s).)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.852Z",
"end_time": "2024-03-16T09:17:34.829000+09:00"
},
"trusted": true
},
"id": "0051fe51",
"cell_type": "code",
"source": "string(mytime2)",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "\"02:46:40 (10000sec(s).)\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"id": "06cac2b7",
"cell_type": "markdown",
"source": "### Point"
},
{
"metadata": {},
"id": "fdce81be",
"cell_type": "markdown",
"source": "+ `MyTime2` 型に特化した `Base.show()` の定義はしない!\n+ その代わり `TimeStyle(::Type{MyTime2}) = WithSeconds()` として「どのようなスタイル(で文字列化する)か」を指定する!"
},
{
"metadata": {},
"id": "1e998eeb",
"cell_type": "markdown",
"source": "## `MyTimeWithMS`"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.853Z",
"end_time": "2024-03-16T09:17:34.832000+09:00"
},
"trusted": true
},
"id": "ae7bb716",
"cell_type": "code",
"source": "struct MyTimeWithMS <: AbstractTime\n hms::MyTime\n ms::Int\n \n MyTimeWithMS(h, m, s, ms) = new(MyTime(h, m, s), ms)\nend\n\ngethour(time::MyTimeWithMS) = gethour(time.hms)\ngetminute(time::MyTimeWithMS) = getminute(time.hms)\ngetsecond(time::MyTimeWithMS) = getsecond(time.hms)\ngetmillisecond(time::MyTimeWithMS) = time.ms\n\nTimeStyle(::Type{MyTimeWithMS}) = WithMS() # ←ココ!",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "TimeStyle"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.853Z",
"end_time": "2024-03-16T09:17:34.974000+09:00"
},
"trusted": true
},
"id": "ab8c902a",
"cell_type": "code",
"source": "mytime3 = MyTimeWithMS(12, 34, 56, 789)",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "12:34:56.789"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.854Z",
"end_time": "2024-03-16T09:17:34.994000+09:00"
},
"trusted": true
},
"id": "88fa633c",
"cell_type": "code",
"source": "string(mytime3)",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "\"12:34:56.789\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Point"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "+ `MyTimeWithMS` 型に特化した `Base.show()` の定義はしない!\n+ その代わり `TimeStyle(::Type{MyTimeWithMS}) = WithMS()` として「どのようなスタイル(で(以下略"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## おまけ:指定したスタイルでの文字列化"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.855Z",
"end_time": "2024-03-16T09:17:34.996000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.string(time::T; style::TimeStyle = TimeStyle(T)) where {T<:AbstractTime} = \n sprint(showtime, style, time)",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.856Z",
"end_time": "2024-03-16T09:17:35.010000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "\"14:28:57\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.856Z",
"end_time": "2024-03-16T09:17:35.023000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime2)",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "\"02:46:40 (10000sec(s).)\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.857Z",
"end_time": "2024-03-16T09:17:35.033000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime3)",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "\"12:34:56.789\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.857Z",
"end_time": "2024-03-16T09:17:35.038000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime, style=Simple()) # == string(mytime)",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "\"14:28:57\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.858Z",
"end_time": "2024-03-16T09:17:35.043000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime2, style=Simple())",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "\"02:46:40\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.858Z",
"end_time": "2024-03-16T09:17:35.051000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime3, style=Simple())",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "\"12:34:56\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.859Z",
"end_time": "2024-03-16T09:17:35.060000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime, style=WithSeconds())",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "\"14:28:57 (52137sec(s).)\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.859Z",
"end_time": "2024-03-16T09:17:35.065000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime2, style=WithSeconds()) # == string(mytime2)",
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 28,
"data": {
"text/plain": "\"02:46:40 (10000sec(s).)\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.860Z",
"end_time": "2024-03-16T09:17:35.074000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime3, style=WithSeconds())",
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "\"12:34:56 (45296sec(s).)\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.860Z",
"end_time": "2024-03-16T09:17:35.087000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime, style=WithMS())",
"execution_count": 30,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 30,
"data": {
"text/plain": "\"14:28:57.000\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.861Z",
"end_time": "2024-03-16T09:17:35.096000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime2, style=WithMS())",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "\"02:46:40.000\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.861Z",
"end_time": "2024-03-16T09:17:35.100000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "string(mytime3, style=WithMS()) # == string(mytime3)",
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 32,
"data": {
"text/plain": "\"12:34:56.789\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"id": "a0646405",
"cell_type": "markdown",
"source": "### Point"
},
{
"metadata": {},
"id": "183320ff",
"cell_type": "markdown",
"source": "+ 下手な継承関係(サブタイピング関係)を作らないことで、好きなスタイルで文字列化できる!\n + 事前に `getseconds()`(`WithSeconds()` で必要)・`getmillisecond()`(`WithMS()` で必要)のデフォルト実装を用意しておいたのはこのため。"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## ベンチマーク"
},
{
"metadata": {
"trusted": true,
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.862Z",
"end_time": "2024-03-16T09:17:35.287000+09:00"
}
},
"id": "cc138757",
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 33,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.863Z",
"end_time": "2024-03-16T09:17:35.365000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "using Random",
"execution_count": 34,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.864Z",
"end_time": "2024-03-16T09:17:39.259000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Random.seed!(1234)\n@benchmark string(MyTime(h, m, s)) setup=(h=rand(0:23);m=rand(0:59);s=rand(0:59))",
"execution_count": 35,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 35,
"data": {
"text/plain": "BenchmarkTools.Trial: 10000 samples with 199 evaluations.\n Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m423.623 ns\u001b[22m\u001b[39m … \u001b[35m 14.229 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 95.84%\n Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m431.015 ns \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m461.747 ns\u001b[22m\u001b[39m ± \u001b[32m401.208 ns\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m4.00% ± 4.48%\n\n \u001b[39m▇\u001b[34m█\u001b[39m\u001b[39m▆\u001b[39m▆\u001b[39m▂\u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m▂\n \u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[32m▇\u001b[39m\u001b[39m▇\u001b[39m▇\u001b[39m▆\u001b[39m▆\u001b[39m▅\u001b[39m▇\u001b[39m▇\u001b[39m▅\u001b[39m▅\u001b[39m▃\u001b[39m▅\u001b[39m▇\u001b[39m▆\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▇\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[39m▇\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▆\u001b[39m▅\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▇\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▃\u001b[39m▁\u001b[39m▁\u001b[39m▃\u001b[39m▅\u001b[39m▃\u001b[39m▅\u001b[39m \u001b[39m█\n 424 ns\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 743 ns \u001b[0m\u001b[1m<\u001b[22m\n\n Memory estimate\u001b[90m: \u001b[39m\u001b[33m536 bytes\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m13\u001b[39m."
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.865Z",
"end_time": "2024-03-16T09:17:43.052000+09:00"
}
},
"cell_type": "code",
"source": "Random.seed!(1234)\n@benchmark string(MyTime2(secs)) setup=(secs=rand(0:60*60*24-1))",
"execution_count": 36,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 36,
"data": {
"text/plain": "BenchmarkTools.Trial: 10000 samples with 177 evaluations.\n Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m597.819 ns\u001b[22m\u001b[39m … \u001b[35m 12.284 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 94.16%\n Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m614.435 ns \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m651.550 ns\u001b[22m\u001b[39m ± \u001b[32m509.720 ns\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m3.85% ± 4.66%\n\n \u001b[39m \u001b[39m▆\u001b[39m█\u001b[34m▆\u001b[39m\u001b[39m▅\u001b[39m▄\u001b[39m▁\u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m▂\n \u001b[39m▇\u001b[39m█\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[32m▆\u001b[39m\u001b[39m█\u001b[39m▆\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▇\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▆\u001b[39m▄\u001b[39m▁\u001b[39m▄\u001b[39m▁\u001b[39m▄\u001b[39m▆\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▆\u001b[39m▇\u001b[39m▅\u001b[39m▄\u001b[39m▆\u001b[39m▆\u001b[39m▆\u001b[39m▆\u001b[39m▇\u001b[39m▇\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▃\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▄\u001b[39m▄\u001b[39m \u001b[39m█\n 598 ns\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 984 ns \u001b[0m\u001b[1m<\u001b[22m\n\n Memory estimate\u001b[90m: \u001b[39m\u001b[33m640 bytes\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m16\u001b[39m."
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"ExecuteTime": {
"start_time": "2024-03-16T00:17:30.865Z",
"end_time": "2024-03-16T09:17:43.052000+09:00"
}
},
"cell_type": "code",
"source": "Random.seed!(1234)\n@benchmark string(MyTimeWithMS(h, m, s, ms)) setup=(h=rand(0:23);m=rand(0:59);s=rand(0:59);ms=rand(0:999))",
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 37,
"data": {
"text/plain": "BenchmarkTools.Trial: 10000 samples with 191 evaluations.\n Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m522.408 ns\u001b[22m\u001b[39m … \u001b[35m 17.747 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 93.98%\n Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m534.283 ns \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m588.141 ns\u001b[22m\u001b[39m ± \u001b[32m745.457 ns\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m7.09% ± 5.38%\n\n \u001b[39m▅\u001b[39m█\u001b[34m▆\u001b[39m\u001b[39m▆\u001b[39m▅\u001b[39m▂\u001b[39m▁\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m▁\n \u001b[39m█\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m▆\u001b[39m▇\u001b[39m▅\u001b[39m▇\u001b[32m▇\u001b[39m\u001b[39m▇\u001b[39m▆\u001b[39m▅\u001b[39m▄\u001b[39m▃\u001b[39m▅\u001b[39m▆\u001b[39m▄\u001b[39m▃\u001b[39m▂\u001b[39m▄\u001b[39m▃\u001b[39m▄\u001b[39m▄\u001b[39m▃\u001b[39m▅\u001b[39m▄\u001b[39m▅\u001b[39m▆\u001b[39m▄\u001b[39m▃\u001b[39m▄\u001b[39m▅\u001b[39m▄\u001b[39m▆\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▅\u001b[39m▃\u001b[39m▄\u001b[39m▅\u001b[39m▂\u001b[39m▃\u001b[39m▄\u001b[39m▃\u001b[39m▄\u001b[39m▃\u001b[39m▂\u001b[39m▄\u001b[39m▃\u001b[39m▄\u001b[39m \u001b[39m█\n 522 ns\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 891 ns \u001b[0m\u001b[1m<\u001b[22m\n\n Memory estimate\u001b[90m: \u001b[39m\u001b[33m768 bytes\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m17\u001b[39m."
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.10",
"display_name": "Julia 1.10.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.10.2"
},
"gist": {
"id": "",
"data": {
"description": "MyTimeByHolyTraits.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
[compat]
julia = "1.6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment