[Python-ideas] is that expensive?

Steven D'Aprano steve at pearwood.info
Sat Feb 22 00:09:50 CET 2014


On Fri, Feb 21, 2014 at 06:37:58PM +0100, Liam Marsh wrote:
> Hello everyone,
> is it possible to create(or tell me its name) a command to evaluate compute
> length of a command/procedure?
> (in number of:             -processor operations(for 32 and 64 bit
> processors)
>                                  -RAM read and write operations
>                                  -hard disk write and read operations
>                                  -eventual graphic operations
>                    )
> this may be difficult, but it ables users to optimise their programs.


What should this is_this_expensive() of this function return?

def func():
    if random.random() < 0.5:
        return "done"
    else:
        while True:
            pass


If you can write an algorithm for deciding:

(1) what does "expensive" mean?

(2) how do you calculate it in advance?

then maybe somebody can create a Python function to perform it. But I 
suspect that it cannot be calculated in advance, there's no 
single meaningful measurement for "expense", and even if there was, it 
would be unhelpful and misleading for optimizing programs.

Instead of trying to predict in advance how expensive a procedure will 
be, why don't you run the procedure and measure how expensive it 
actually is? See the profile module for ways to measure how much time is 
used by different parts of the code.

-- 
Steven


More information about the Python-ideas mailing list