# What would be the equivalent of this piece of Python code?

**URL:** <https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827>\
**Category:** General Usage\
**Tags:** question, first-steps\
**Created:** [February 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827 "2020-02-18T20:01:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/1 "2020-02-18T20:01:06Z")

</div>

Hi

I have used this piece of code to measure the time it takes for a Python script to execute.  
I want to do the same with Julia.

```
from datetime import datetime
start_time = datetime.now()

end_time = datetime.now()
delta_time = end_time - start_time
print(delta_time.microseconds)

```

Is there an equivalent in Julia?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [February 18, 2020, 8:12pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/2 "2020-02-18T20:12:19Z")

</div>

~~where is `delta_time` defined in that python code? Do you mean the difference between `start_time` and some `end_time`? If so,~~

```julia
using Dates
start_time = now()
...
end_time = now()
elapsed = end_time - start_time

```

will give you `elapsed` as a type representing the elapsed time in some interval (`Milliseconds`) from which you can extract the value as an `Int` using `elapsed.value`.

(edited based on edit to question)

However, you should also look into the `@time` macro, and `@btime` and `@benchmark` from `BenchmarkTools.jl` for other options.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [February 18, 2020, 8:14pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/3 "2020-02-18T20:14:44Z")

</div>

If you want to benchmark julia code, you should try:

> **[GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia...](https://github.com/JuliaCI/BenchmarkTools.jl)**
>
> A benchmarking framework for the Julia language. Contribute to JuliaCI/BenchmarkTools.jl development by creating an account on GitHub.

Which is much better than a simple `elapsed = end_time - start_time`, which will measure compilation times and is statistically not very robust …  
If you’re wondering why your Julia code is slow, consider:  
[https://docs.julialang.org/en/v1/manual/performance-tips/index.html](https://docs.julialang.org/en/v1/manual/performance-tips/index.html)

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 18, 2020, 9:58pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/4 "2020-02-18T21:58:28Z")

</div>

Can you explain further on “statistically not very robust”?

What will happen if I measure compilation time?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [February 18, 2020, 10:10pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/5 "2020-02-18T22:10:20Z")

</div>

> Can you explain further on “statistically not very robust”?

It means that a sample size of 1 (which is what you’re going to get) is neither meaningful nor conclusive, as you’re introducing unknown system state (among other variables) into your experiment. BenchmarkTools gets around this by running the code multiple times and provides statistics across its entire run.

> What will happen if I measure compilation time?

You will get a tremendously pessimistic result, as the second run of a function is significantly faster:

```julia

julia> using LightGraphs
julia> g = Graph(100,200)
{100, 200} undirected simple Int64 graph
julia> @time betweenness_centrality(g); # timing compilation since this is the first run
  0.292876 seconds (602.88 k allocations: 32.449 MiB)
julia> @time betweenness_centrality(g); # already compiled
  0.002362 seconds (15.46 k allocations: 2.427 MiB)

```

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 24, 2020, 3:57pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/6 "2020-02-24T15:57:36Z")

</div>

You are right. I tested “Dates” with my code in Julia. And “Datetime” in Python. The first time Julia is slowly. But even despite of the compilation time it is faster than Python.  
I did this test only 4 times. I would like to do it at leats 1000 times. Do you know of a similar tool like “BenchmarkTools” but in Python?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 24, 2020, 4:10pm UTC](https://discourse.julialang.org/t/what-would-be-the-equivalent-of-this-piece-of-python-code/34827/7 "2020-02-24T16:10:07Z")

</div>

Yes, the `timeit` module:  
[https://docs.python.org/3.8/library/timeit.html](https://docs.python.org/3.8/library/timeit.html)
