@why posted a new topic, subject "Timing code execution". It read:

   Hello,
    
    I just ran the following code to time how long a part of a function takes to execute:
    [code] 
        time1 = NaiveDateTime.utc_now
        
        ...some code or function call...
        
        time2 = NaiveDateTime.utc_now
        
        time_total = NaiveDateTime.diff(time2, time1, :microsecond)
        
    [/code]
    
    `time_total` came back remarkably low (two-digit microseconds). Quite plausible (and fascinating!), but might I be missing something with this logic, particularly if a function call is between time1 and time2?
    
    Thank you.

After which, @tcoopman added

    If you want to do a micro benchmark (measure how long a function takes), doing one run of the function is not enough.  
    You can take a look at https://github.com/bencheeorg/benchee to run microbenchmarks.

@why replied

    Thank @tcoopman, great advice, looking into Benchee now.

To which @why replied

    …and now also doing my own “quick stats” by just running the code multiple times and getting a rough sense for the distribution of time outcomes as well as some average

After which, @NobbZ (Norbert Melzer) added

    Even though there are indeed benchmarking frameworks available, there is also `:timer.tc/1,2,3` in Erlang.
    
    For one time measurements, eg. for some progress report or sending to a metrics service, it's usually sufficient.

So @why replied

    Thank you, @NobbZ!


{'source': 'discourse', 'url': 'https://elixirforum.com/t/timing-code-execution/36286', 'title': 'Timing code execution', 'views': 1227, 'has_accepted_answer': False, 'word_count': 194, 'like_count': 3, 'reply_count': 3, 'participant_count': 3, 'posts': 6, 'tags': 'datetime', 'author': '@why', 'participants': '@why, @tcoopman, @NobbZ (Norbert Melzer)', 'image_url': [None], 'created_at': '2020-12-17T10:12:58.557Z', 'last_posted_at': '2020-12-17T11:39:32.731Z'}