2

So, I have recently been tasked with writing a function in python 2 that can time the execution of another function. This is simple enough, but the catch is I have to do it WITHOUT importing any modules; this naturally includes time, timeit, etc.

Using only built in functions and statements (e.g. sum(), or, yield) is this even possible?

I don't want to see a solution, I need to work that out for myself, but I would greatly appreciate knowing if this is even possible. If not, then I'd rather not waste the time bashing my head against the proverbial brick wall.

Jgd10
  • 497
  • 2
  • 14
  • 2
    I **really** don't think this is possible. None of the builtin functions have anything to do with time or the CPU clock. – internet_user Dec 19 '17 at 01:06
  • 4
    Is this requirement because of a specific test, or are there some specific reasons for no imports? If it's a test, is it a serious test (university / job quiz), or a "use whatever ideas you have" test? Also, what OS are you running on? Also, also, what kind of accuracy are you looking for? – viraptor Dec 19 '17 at 01:07
  • Depends. Do you know what system this is running on? – Alyssa Haroldsen Dec 19 '17 at 01:09
  • You could sidestep the question and use `line_profiler` module. It doesn't require any imports, only `@profile` decorators on your functions and a call from the command line... Since we've established that it's not possible in the traditional sense. Technically it's a different script and your script is the target it reads. – roganjosh Dec 19 '17 at 01:17
  • interview question; but it could be a poorly phrased one, especially if it's actually impossible. They may simply mean don't use any time-related modules but you can import other stuff? Although they do state in the question that incomplete answers are interesting too, perhaps they don't expect an answer? – Jgd10 Dec 19 '17 at 01:20
  • 1
    @roganjosh yeah something like that could work! I'll look into it. – Jgd10 Dec 19 '17 at 01:21
  • Said it as a bit of a joke, before I realised it was an interview question :P But I always struggle to get it to work properly on Windows so I end up just downloading the source files and copy/pasting them into the directory of something I want to test. IIRC the command is along the lines of `python line_profiler.py -l -v my_file.py > test_output.txt`. Still, it doesn't _require_ the import of any modules in your main script that you're testing. You also need `kernprof.py` in the same directory. – roganjosh Dec 19 '17 at 01:27
  • There was a "hot" question about a nonsensical question recently. Let me see if I can find it. – roganjosh Dec 19 '17 at 01:32
  • It's [here](https://stackoverflow.com/q/47581326/4799172). I made a comment on the question only. Once I went for an 8 hour interview for a chemical engineering consultancy. I lost the job because of a question on a balloon in the back of a van question (literally). They gave me a report and I passed every other question. The issue was that I didn't reason my way through an ill-defined problem properly, I jumped to conclusions. It's a growing tactic to put people off-guard. – roganjosh Dec 19 '17 at 01:37
  • If it's an active question; show your best attempt, explain why you think it's impossible, and show your best attempt at solving it (plus add some caveats for more elaborate solutions if the data you need is not available but _could_ work if it was). The whole point of this kind of question is to see how you reason out an impossible problem, not to see how you flip (or, actually, flipping is a good signal that you're not for the job). – roganjosh Dec 19 '17 at 01:48
  • 1
    Agreed, thank you for the advice! (Deleted my comment by accident.) – Jgd10 Dec 19 '17 at 01:59

4 Answers4

4

If you're on a UNIX (or maybe just Linux) system, yes. Read from /proc/uptime. It's not super efficient, but hey, builtin functions only. I'm not sure of a way to do this on Windows.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
3

Simple answer: No, it is not possible.

Here you have a link to the python 2.7 built in functions docs. None of them allow you to measure time. You are forced to use a module.

Python was thought to be used with its modules and it includes a great amalgam of them. I would recomend you to use time for this one.

Sorry for breaking your dreams <3

Yadkee
  • 66
  • 1
  • 5
2

Depending on the OS you're running and how messy solution can you accept, you can do this without imports.

Ordered by increasing insanity:

  1. Some systems provide virtual files which contain various timers. You can get a sub-second resolution at least on a Linux system by reading a counter from that kind of file before and after execution. Not sure about others.

  2. Can you reuse existing imports? If the file already contains any of threading, multiprocessing, signal, you can construct a timer out of them.

  3. If you have some kind of scheduler running on your system (like cron) you can inject a job into it (by creating a file), which will print out timestamps every time it's run.

  4. You can follow a log file on a busy system and assume the last message was close to the time you read it.

  5. Depending on what accuracy you want, you could measure the amount of time each python bytecode operation takes, then write an interpreter for the code available via function.__code__.co_code. While you run the code, you can sum up all the expected execution times. This is the only pure-python solution which doesn't require a specific OS / environment.

  6. If you're running on a system which allows process memory introspection, you can open it and inject any functionality without technically importing anything.

viraptor
  • 33,322
  • 10
  • 107
  • 191
1

Two "cheating" methods.

If you're avoiding the import keyword, you can use __import__ to import time, which is actually a module builtin to the python2 executable.

If you know the location of the Python installation, you can use execfile on os.py and use the times function.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35