0

My task is to measure time of a function which contains an input inside (the user is to input a list).
There is the code:

import numpy
import itertools
def amount(C):
    N = numpy.array(input().strip().split(" "),int)
    N = list(N)
    N = sorted(N)
    while C < max(N):
        N.remove(max(N))
    res = []
    for i in range(1, C):
        for j in list(itertools.combinations_with_replacement(N, i)):
            res.append(sum(list(j)))
    m = 0
    for z in range (0, len(res)):
        if res[z] == C:
            m += 1
    if N[0] == 1:
        return m + 1 
    else:
        return m 

Of course it is not optimized (but it's not the case now).
Because of the "list" standard way by which I mean:

import time
start = time.time()
amount(10)
end = time.time()
print(end - start)

does not work.
How can I measure the time? For example for:

C in range [0, 11]
N = [1, 2 , 5]

I would be grateful for any help! :)

2 Answers 2

1

You want somethig like this?

import time

# measure process time
t0 = time.clock()
amount(10)
print time.clock() - t0, "seconds process time"

# measure wall time
t0 = time.time()
amount(10)
print time.time() - t0, "seconds wall time"

UPDATE : The soulution maybe could be this :

times=[] 
for x in range(12):
  t0 = time.time()
  amount(x) 
  times.append( time.time() - t0);

or better if you use timeit : here

Sign up to request clarification or add additional context in comments.

3 Comments

No, I need something else I think. I would like a function which returns 9 measurements (each for different C in range [0, 10].
Ok i understand, give me 1 sec
Ok, take your time
0

What do you think about this:

import time
def time_counter(amount, n=100, M=100):
    res = list(range(n))
    def count_once():
        start = time.perf_counter()
        amount(res)
        return time.perf_counter() - start
    return [count_once() for m in range(M)]

It is to make M = 1000 measurements and for C in range(0, n). Is it alright?

1 Comment

Actually after some time I realized that it doesn't work lol Any ideas how to improve that function? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.