Home > Enterprise >  How to measure time of a While's condition (in Python). Need it to measure SAT search in Z3-Py
How to measure time of a While's condition (in Python). Need it to measure SAT search in Z3-Py

Time:03-22

I am trying to measure the execution time of a Python program. For this, I am using the perf_counter() library.

My problem is not with respect to the library, but with respect to how to measure a concrete case.

The thing is that my program contains a while, and I am particularly interested in knowing how much execution time the while's condition consumes (for those interested: this is because the condition of the while is that a very long logical formula is satisfiable, so this sat search can be expensive).

My question is, how can I calculate this?

I mean, I could (as you can see below) re-execute the condition and save it but that is not a solution for me because (1) the time could have been different from that of the execution of the while condition and, above all, (2) it would be preferable not to re-execute code, as this falsifies the real time of the total execution.

t_start = perf_counter()
...
t_condition = 0
while condition:
  t_conditionStart = perf_counter()
  condition #measure this one? I do not like this!
  t_conditionStop = perf_counter()
  t_condition  = t_conditionStop - t_conditionStart
  ...
...
t_stop = perf_counter()
total_time = t_stop - t_start
condition_percentage = t_condition / total_time

I don't know if I'm missing something very basic.

For the ones interested in the real SAT problem (using Z3-Py), the code should go (or better said: should not go) like this:

t_start = perf_counter()
...
t_satSearch = 0
s = Solver()
while s.check() == sat:
  t_satSearchStart = perf_counter()
  s.check() == sat #measure this one? I do not like this!
  t_satSearchStop = perf_counter()
  t_satSearch  = t_satSearchStop - t_satSearchStart
  ...
...
t_stop = perf_counter()
total_time = t_stop - t_start
satSearch_percentage = t_satSearch / total_time

CodePudding user response:

Condition is checked before each execution of the loop. So, all you need to do is to is to measure time in 4 places:

  1. before the loop (t0)
  2. at the beginning of the loop (t1)
  3. right before loop finishes (t0)
  4. after the while loop (t1)

This way you can get the execution time in every possible case by checking what is the value of t1-t0 every time you update t1.

  • Related