Get average run time from `%timeit` ipython magic

时间:2016-02-12 20:14:08

标签: python ipython timeit

Trying to time different random functions to see fastest way to choose random item from list. PROCEDURE Test_script (OUT XYZRow) BEGIN DECLARE Veiw_Name VARCHAR(2147483647); DECLARE Object_Name VARCHAR(2147483647); DECLARE Object_Type VARCHAR(2147483647); DECLARE Domain_Name VARCHAR(2147483647); DECLARE Status VARCHAR(2147483647); DECLARE "READ" VARCHAR(100); DECLARE PUBLIC SetException EXCEPTION; for r as select * from table1 do for r2 as SELECT r1.name as Object_Nam, r1.nameType as Object_Typ, r1."domain" as domain_nam, CASE r1.Status AS Status, CASE r1.c_R AS READ_Stat FROM function_xyz(r.PATH) r1 do set Veiw_Name = r.PATH; set Object_Name = r2.Object_Nam; set Object_Type = r2.Object_Typ; set Domain_Name = r2.domain_nam; set Status = r2.Status; set "READ" = r2.Read_Stat; INSERT INTO XYZRow VALUES (Veiw_Name, Object_Name, Object_Type, Domain_Name, Status, "READ"); end for; end for; EXCEPTION WHEN System.SystemException THEN CALL PRINT(CURRENT_EXCEPTION.MESSAGE); END wants to give me "best of 3" fastest times, but because the runs are random, there's high variance in access times (grab from back of list, will be slow; grab from front, will be fast).

How do I get average across all loops, not best of?

%timeit

Currently output (acknowledging variance in timing):

a = [0,6,3,1,3,9,4,3,2,6]

%timeit random.choice(a)
%timeit a[random.randint(0,len(a)-1)]
%timeit a[np.random.randint(0,len(a)-1)]
%timeit np.random.choice(a,1)[0]

Update: a kludge approach:

%timeit random.choice(a)
The slowest run took 9.87 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 1.23 µs per loop

2 个答案:

答案 0 :(得分:2)

You could use timeit.repeat:

$$('select#shipping_method option').last().selected=true;
$$('select#shipping_method option').last().simulate('change');

One potential issue is that you are likely to run into caching effects that could render your timings less meaningful (see here). You might, for example, want to generate a new random list on each iteration using the import timeit import numpy as np reps = timeit.repeat(repeat=3, n=10000, stmt="np.random.choice(a)", setup="import numpy as np; a=[0,6,3,1,3,9,4,3,2,6]") # taking the median might be better, since I suspect the distribution of times will # be heavily skewed avg = np.mean(reps) argument.

答案 1 :(得分:0)

How fast is

@IBAction func onDo(sender:UIButton)
{
    self.view.setNeedsLayout()
    self.testConstraint.constant = 40.0

    UIView.animateWithDuration(2.0, animations: { () -> Void in

        self.view.setNeedsLayout()

        }) { (complete:Bool) -> Void in

    }
}

for you? I'd just generate a huge amount of random numbers at once if that is your bottleneck.

In my aged PC:

random_fd = open('/dev/urandom', 'rb')

a = array.array('I')
a.read(random_fd, 10**8)

get_next_rand = iter(a).next