Skip to content

Commit 1b02338

Browse files
authored
fix #17046, deprecate tic and toc (#23773)
1 parent dd0f1f6 commit 1b02338

File tree

13 files changed

+80
-132
lines changed

13 files changed

+80
-132
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ Deprecated or removed
453453
`prompt_if_incorrect` argument are deprecated. Instead, prompting behavior is controlled using
454454
the `allow_prompt` keyword in the `LibGit2.CredentialPayload` constructor ([#23690]).
455455

456+
* The timing functions `tic`, `toc`, and `toq` are deprecated in favor of `@time` and `@elapsed`
457+
([#17046]).
458+
456459
Command-line option changes
457460
---------------------------
458461

base/deprecated.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,37 @@ end
18151815
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)
18161816
end
18171817

1818+
export tic, toq, toc
1819+
function tic()
1820+
depwarn("tic() is deprecated, use @time, @elapsed, or calls to time_ns() instead.", :tic)
1821+
t0 = time_ns()
1822+
task_local_storage(:TIMERS, (t0, get(task_local_storage(), :TIMERS, ())))
1823+
return t0
1824+
end
1825+
1826+
function _toq()
1827+
t1 = time_ns()
1828+
timers = get(task_local_storage(), :TIMERS, ())
1829+
if timers === ()
1830+
error("toc() without tic()")
1831+
end
1832+
t0 = timers[1]::UInt64
1833+
task_local_storage(:TIMERS, timers[2])
1834+
(t1-t0)/1e9
1835+
end
1836+
1837+
function toq()
1838+
depwarn("toq() is deprecated, use @elapsed or calls to time_ns() instead.", :toq)
1839+
return _toq()
1840+
end
1841+
1842+
function toc()
1843+
depwarn("toc() is deprecated, use @time, @elapsed, or calls to time_ns() instead.", :toc)
1844+
t = _toq()
1845+
println("elapsed time: ", t, " seconds")
1846+
return t
1847+
end
1848+
18181849
@noinline function getaddrinfo(callback::Function, host::AbstractString)
18191850
depwarn("getaddrinfo with a callback function is deprecated, wrap code in @async instead for deferred execution", :getaddrinfo)
18201851
@async begin

base/exports.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,11 +897,8 @@ export
897897

898898
# time
899899
sleep,
900-
tic,
901900
time,
902901
time_ns,
903-
toc,
904-
toq,
905902

906903
# dates
907904
Date,

base/util.jl

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -76,79 +76,6 @@ gc_time_ns() = ccall(:jl_gc_total_hrtime, UInt64, ())
7676
# total number of bytes allocated so far
7777
gc_bytes() = ccall(:jl_gc_total_bytes, Int64, ())
7878

79-
"""
80-
tic()
81-
82-
Set a timer to be read by the next call to [`toc`](@ref) or [`toq`](@ref). The
83-
macro call `@time expr` can also be used to time evaluation.
84-
85-
```julia-repl
86-
julia> tic()
87-
0x0000c45bc7abac95
88-
89-
julia> sleep(0.3)
90-
91-
julia> toc()
92-
elapsed time: 0.302745944 seconds
93-
0.302745944
94-
```
95-
"""
96-
function tic()
97-
t0 = time_ns()
98-
task_local_storage(:TIMERS, (t0, get(task_local_storage(), :TIMERS, ())))
99-
return t0
100-
end
101-
102-
"""
103-
toq()
104-
105-
Return, but do not print, the time elapsed since the last [`tic`](@ref). The
106-
macro calls `@timed expr` and `@elapsed expr` also return evaluation time.
107-
108-
```julia-repl
109-
julia> tic()
110-
0x0000c46477a9675d
111-
112-
julia> sleep(0.3)
113-
114-
julia> toq()
115-
0.302251004
116-
```
117-
"""
118-
function toq()
119-
t1 = time_ns()
120-
timers = get(task_local_storage(), :TIMERS, ())
121-
if timers === ()
122-
error("toc() without tic()")
123-
end
124-
t0 = timers[1]::UInt64
125-
task_local_storage(:TIMERS, timers[2])
126-
(t1-t0)/1e9
127-
end
128-
129-
"""
130-
toc()
131-
132-
Print and return the time elapsed since the last [`tic`](@ref). The macro call
133-
`@time expr` can also be used to time evaluation.
134-
135-
```julia-repl
136-
julia> tic()
137-
0x0000c45bc7abac95
138-
139-
julia> sleep(0.3)
140-
141-
julia> toc()
142-
elapsed time: 0.302745944 seconds
143-
0.302745944
144-
```
145-
"""
146-
function toc()
147-
t = toq()
148-
println("elapsed time: ", t, " seconds")
149-
return t
150-
end
151-
15279
# print elapsed time, return expression value
15380
const _mem_units = ["byte", "KiB", "MiB", "GiB", "TiB", "PiB"]
15481
const _cnt_units = ["", " k", " M", " G", " T", " P"]

doc/src/manual/noteworthy-differences.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ may trip up Julia users accustomed to MATLAB:
5252
* In Julia, functions such as [`sort`](@ref) that operate column-wise by default (`sort(A)` is
5353
equivalent to `sort(A,1)`) do not have special behavior for `1xN` arrays; the argument is returned
5454
unmodified since it still performs `sort(A,1)`. To sort a `1xN` matrix like a vector, use `sort(A,2)`.
55-
* In Julia, parentheses must be used to call a function with zero arguments, like in [`tic()`](@ref)
56-
and [`toc()`](@ref).
55+
* In Julia, parentheses must be used to call a function with zero arguments, like in [`rand()`](@ref).
5756
* Julia discourages the used of semicolons to end statements. The results of statements are not
5857
automatically printed (except at the interactive prompt), and lines of code do not need to end
5958
with semicolons. [`println`](@ref) or [`@printf`](@ref) can be used to print specific output.

doc/src/manual/performance-tips.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ julia> @time f(10^6)
7373
On the first call (`@time f(1)`), `f` gets compiled. (If you've not yet used [`@time`](@ref)
7474
in this session, it will also compile functions needed for timing.) You should not take the results
7575
of this run seriously. For the second run, note that in addition to reporting the time, it also
76-
indicated that a large amount of memory was allocated. This is the single biggest advantage of
77-
[`@time`](@ref) vs. functions like [`tic`](@ref) and [`toc`](@ref), which only report time.
76+
indicated that a large amount of memory was allocated.
7877

7978
Unexpected memory allocation is almost always a sign of some problem with your code, usually a
8079
problem with type-stability. Consequently, in addition to the allocation itself, it's very likely

doc/src/stdlib/base.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ Base.getipaddr
193193
Base.Libc.getpid
194194
Base.Libc.time()
195195
Base.time_ns
196-
Base.tic
197-
Base.toc
198-
Base.toq
199196
Base.@time
200197
Base.@timev
201198
Base.@timed

test/channels.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ end
212212
@async begin sleep(1.0); put!(rr2, :ok) end
213213
@async begin sleep(2.0); put!(rr3, :ok) end
214214

215-
tic()
216-
timedwait(callback, Dates.Second(1))
217-
et=toq()
215+
et = @elapsed timedwait(callback, Dates.Second(1))
216+
218217
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU
219218
try
220219
@assert (et >= 1.0) && (et <= 1.5)

test/file.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ function test_file_poll(channel,interval,timeout_s)
192192
end
193193

194194
function test_timeout(tval)
195-
tic()
196-
channel = Channel(1)
197-
@async test_file_poll(channel, 10, tval)
198-
tr = take!(channel)
199-
t_elapsed = toq()
195+
t_elapsed = @elapsed begin
196+
channel = Channel(1)
197+
@async test_file_poll(channel, 10, tval)
198+
tr = take!(channel)
199+
end
200200
@test tr[1] === Base.Filesystem.StatStruct() && tr[2] === EOFError()
201201
@test tval <= t_elapsed
202202
end

test/perf/kernel/gk.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function gk(n, myeps)
6565

6666
csum = zeros(n)
6767

68-
# tic()
6968
while(stop != 1)
7069
t=t+1
7170
iter=t
@@ -115,7 +114,7 @@ function gk(n, myeps)
115114

116115
end
117116

118-
times[KK] = 0#toc()
117+
times[KK] = 0
119118
iteration[KK] = iter
120119

121120
x = X/t

0 commit comments

Comments
 (0)