Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jesse Wursten
    started a topic -timeit- now on SSC: A same-line timer

    -timeit- now on SSC: A same-line timer

    Dear all

    -timeit- is now available on SSC, thanks as usual to Kit Baum. It is a simple wrapper program that replaces
    Code:
    timer on 1
    reg y x [note: any command here works, this is just an example]
    timer off 1
    by

    Code:
    timeit 1: reg y x
    In my opinion its main advantage is that your code can still look clean even with timers - the timer on/off lines made everything look super messy in my opinion, making it harder to debug or share code.

    Currently the timer will keep running if your command returns an error. I will hopefully fix this in a future version, though suggestions on how to do this are welcome.

  • Jesse Wursten
    replied
    Updated such that named timers progress independently from numbered ones. In the example below, the "regression" timer cumulates to 1500ms, despite being linked to different numbered timers in each call.

    Inspired by this post.

    Thanks to Kit Baum as usual for the ssc update.

    Code:
    . timeit 1 regression: sleep 1000
    . timeit 2 regression: sleep 500
    . return list scalars:
       r(delta_regression) =  .509
             r(regression) =  1.512
               r(delta_t2) =  .509
                     r(t2) =  .509
               r(delta_t1) =  1.003
                     r(t1) =  1.003
    Last edited by Jesse Wursten; 06 Oct 2022, 00:38.

    Leave a comment:


  • Jesse Wursten
    replied
    daniel klein Yes!!!!

    Sorry, I have been looking for a way to respect the host version in a robust way for a long time now and this does that brilliantly.

    I've adapted the code, it's en route to SSC now (and attached)
    Attached Files

    Leave a comment:


  • daniel klein
    replied
    Ali correctly diagnoses the problem; I suggest a different solution, though. Do not tinker with timeit.ado. Instead, change your programs to properly include the desired version, i.e., write

    Code:
    program define my_program
                version 12 // <- add the version
                sysuse "auto.dta"
                ren weigh* weigh
    end
    Alternatively, type

    Code:
    timeit 1 : version 12 : my_program

    Jesse might want to add caller-version support (something that I tend to forget, too). The relevant changes might be as simple as

    Code:
    [...]
    nobreak {
        timer on `theTimer'
        capture noisily break version `=_caller()' : `theCommand'
        timer off `theTimer'
    }
    [...]

    Leave a comment:


  • Justin Niakamal
    replied
    Does the trick. Thanks, Ali.

    Leave a comment:


  • Ali Atia
    replied
    I believe the problem is the version statement. The error doesn't appear when the statement is changed to version 12, which is probably because additional functionality was added to rename group at that time. I'm not sure if changing the version breaks anything else, but if you want a temporary workaround that does the trick.
    Last edited by Ali Atia; 09 Jun 2021, 14:52.

    Leave a comment:


  • Justin Niakamal
    replied
    Yes, that's covered in the first post of the thread. My question was in the context of timeit.

    Leave a comment:


  • Leonardo Guizzetti
    replied
    The usual way would be marginally more typing.

    Code:
    timer on 1
    ...your code...
    timer off 1
    timer list 1

    Leave a comment:


  • Justin Niakamal
    replied
    I’m interested in using timeit before running a do file, but I’m getting an error and I think the wildcard asterisk is causing the issue. Here are two simplified examples

    Code:
    clear all
     
    program define my_program
                sysuse "auto.dta"
                ren weigh* weigh
    end
     
    program define my_program_alt
                sysuse "auto.dta"
                rename *, lower
    end
     
     
     
    timeit 1: my_program
    timeit 2: my_program_alt
    Code:
    . timeit 1: my_program
    (1978 Automobile Data)
    * invalid name
    r(198);
     
    . timeit 2: my_program_alt
    (1978 Automobile Data)
    * invalid name
    r(198);
    Is there a workaround?

    Leave a comment:


  • Jesse Wursten
    replied
    Nice catch. This should be fixed in the version on its way to SSC now. I've also attached it here.
    Attached Files

    Leave a comment:


  • Joro Kolev
    replied
    -timeit- returns an error when there is empty space before the semicolon.

    E.g.,

    Code:
    . clear
    
    . timer clear
    
    . set obs 10
    number of observations (_N) was 0, now 10
    
    . timeit 1 : gen u = runiform()
    = invalid name
    r(198);
    
    .
    This is not an end-of-the-world problem, but it is a bit inconsistent with usual Stata behaviour where additional empty spaces typically do not cause issues. I do use empty spaces a lot to improve readability of code.

    Leave a comment:


  • Jesse Wursten
    replied
    Hmm, you seem to be correct. I should be able to fix this.

    EDIT: I think I fixed it. Updated version is en route to SSC, also attached below.
    Attached Files
    Last edited by Jesse Wursten; 27 Nov 2018, 03:19.

    Leave a comment:


  • Santiago Cantillo
    replied
    Your right, I tried that and it worked. But if the name of the do file has spaces it won't work.
    Code:
     
     timeit 1: do "sleep 2000.do"
    produces type mismatch "r(109)".
    I also received this error message when trying to merge a file that had spaces in the name.
    Perhaps the issue occurs when doing any operation involving file names with spaces?

    Leave a comment:


  • Jesse Wursten
    replied
    Originally posted by Santiago Cantillo View Post
    Hi Jesse Wursten . Thanks very much for your program.
    It just occured to me that it would also be really helpful if one could use timeit before running external do files
    (e.g. timeit 1 dofile1 : do foo.do).
    Code:
    timeit 1: do sleep2000.do
    That worked for me?

    Leave a comment:


  • Santiago Cantillo
    replied
    Hi Jesse Wursten . Thanks very much for your program.
    It just occured to me that it would also be really helpful if one could use timeit before running external do files
    (e.g. timeit 1 dofile1 : do foo.do).

    Leave a comment:

Working...
X