Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of the time module #1680

Merged
merged 4 commits into from
Apr 8, 2023
Merged

Conversation

gptsarthak
Copy link
Contributor

For timing the benchmarks, we currently use the bash time command. But that only provides the time in millisecond. Python's time.time() method outputs time in a much higher precision.

I have implemented the time.time() method here using Unix Time after understanding timemodule.c

Here is an example:

import time

def fibonacci(n : i32) -> i32:
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

def main():
    print("Current time : ", time.time())
    start_time : f64 = time.time()
    a : i32 = fibonacci(35)
    print("Answer: ", a)
    print("Time taken : ", time.time()-start_time)

main()
(base) sarthak@pop-os:~/lpython$ python examples/timetest.py
Current time :  1680876878.893159
Answer:  9227465
Time taken :  2.6417429447174072
(base) sarthak@pop-os:~/lpython$ lpython examples/timetest.py
Current time :  1.68087688629948306e+09          // Decimal : 1680876886.29948306
Answer:  9227465
Time taken :  7.29780197143554688e-02            // Decimal : 0.0729780197143554688

I will add tests after a review.

@certik
Copy link
Contributor

certik commented Apr 7, 2023

@gptsarthak perfect, thanks for this! I left some comments.

@gptsarthak gptsarthak marked this pull request as draft April 8, 2023 12:30
@gptsarthak
Copy link
Contributor Author

I have removed the use of <sys/time> and added the implementation of time for Windows. However, this needed us to include a new header <winsock2.h> (a part of <windows.h>).

CPython Reference used for implementation : pytime.c#L871

Why not using the _lfortran_cpu_time function that is already implemented? It should work and at least it compiles on Windows, even if not implemented yet.

I could not use that function becuase it uses clock() from time.h which does not give the same output as CPython.
Same example ran in Windows:

(lp) C:\Users\gptsa\lpython>python examples\timetest.py
Current time :  1680962365.4241385
Answer:  9227465
Time taken :  6.094292402267456

(lp) C:\Users\gptsa\lpython>src\bin\lpython examples\timetest.py
Current time :  1.68096238567989349e+09
Answer:  9227465
Time taken :  1.15024566650390625e-01

@gptsarthak gptsarthak marked this pull request as ready for review April 8, 2023 16:24
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good, thanks!

@certik certik merged commit 7d78dba into lcompilers:main Apr 8, 2023
@certik
Copy link
Contributor

certik commented Apr 8, 2023

@gptsarthak do you want to add some tests in a subsequent PR?

@gptsarthak gptsarthak deleted the time branch April 13, 2023 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants