timing

Java performance timing library

拟墨画扇 提交于 2019-11-27 00:55:57
问题 I frequent wrap code in a System.nanoTime() pair in order to timing it. Something like: long start = System.nanoTime(); methodToBeTimed(); long elapsedTime = System.nanoTime() - start; There is any good timing library that helps with this problem? Also homegrown code will be accepted. NB A profiler is not a solution here, since I want to enforce some time constraints in my unit tests, so I want to timing methods programmatically . 回答1: I haven't used it but I came across perf4j recently. 回答2:

differences between “d = dict()” and “d = {}”

浪尽此生 提交于 2019-11-27 00:10:44
问题 $ python2.7 -m timeit 'd={}' 10000000 loops, best of 3: 0.0331 usec per loop $ python2.7 -m timeit 'd=dict()' 1000000 loops, best of 3: 0.19 usec per loop Why use one over the other? 回答1: I'm one of those who prefers words to punctuation -- it's one of the reasons I've picked Python over Perl, for example. "Life is better without braces" (an old Python motto which went on a T-shirt with a cartoon of a smiling teenager;-), after all (originally intended to refer to braces vs indentation for

Best timing method in C?

纵然是瞬间 提交于 2019-11-26 23:35:46
What is the best way to time a code section with high resolution and portability? /* Time from here */ ProcessIntenseFunction(); /* to here. */ printf("Time taken %d seconds %d milliseconds", sec, msec); Is there a standard library that would have a cross-platform solution? I think this should work: #include <time.h> clock_t start = clock(), diff; ProcessIntenseFunction(); diff = clock() - start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf("Time taken %d seconds %d milliseconds", msec/1000, msec%1000); gettimeofday() will probably do what you want. If you're on Intel hardware, here's how

Is setTimeout with no delay the same as executing the function instantly?

99封情书 提交于 2019-11-26 22:26:43
I am looking at some existing code in a web application. I saw this: window.setTimeout(function () { ... }) Is this the same as just executing the function content right away? angusC It won't necessarily run right away, neither will explicitly setting the delay to 0. The reason is that setTimeout removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue. console.log(1); setTimeout(function() {console.log(2)}); console.log(3); console.log(4); console.log(5); //console logs 1,3,4,5,2 for more details see http:/

timeit versus timing decorator

*爱你&永不变心* 提交于 2019-11-26 21:53:29
I'm trying to time some code. First I used a timing decorator: #!/usr/bin/env python import time from itertools import izip from random import shuffle def timing_val(func): def wrapper(*arg, **kw): '''source: http://www.daniweb.com/code/snippet368.html''' t1 = time.time() res = func(*arg, **kw) t2 = time.time() return (t2 - t1), res, func.__name__ return wrapper @timing_val def time_izip(alist, n): i = iter(alist) return [x for x in izip(*[i] * n)] @timing_val def time_indexing(alist, n): return [alist[i:i + n] for i in range(0, len(alist), n)] func_list = [locals()[key] for key in locals()

How to estimate SQL query timing?

狂风中的少年 提交于 2019-11-26 16:00:11
问题 I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take: mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS NULL; +----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------------

Python class @property: use setter but evade getter?

て烟熏妆下的殇ゞ 提交于 2019-11-26 15:31:01
问题 In python classes, the @property is a nice decorator that avoids using explicit setter and getter functions. However, it comes at a cost of an overhead 2-5 times that of a "classical" class function. In my case, this is quite OK in the case of setting a property, where the overhead is insignificant compared to the processing that needs to be done when setting. However, I need no processing when getting the property. It is always just "return self.property". Is there an elegant way to use the

How to get millisecond and microsecond-resolution timestamps in Python [duplicate]

假如想象 提交于 2019-11-26 14:31:00
问题 This question already has answers here : High-precision clock in Python (13 answers) Closed last year . I finally figured this out and would like to share the knowledge and save someone a bunch of time, so see my answer below. However, I still need answers for Linux, so please answer if you know, as my code in my answer is for Windows only. UPDATE: I've figured it out for Linux too, including for pre-Python 3.3 (ex: for the Raspberry Pi), and I've posted my new module/code in my answer below.

Monotonic clock on OSX

纵然是瞬间 提交于 2019-11-26 13:57:55
问题 CLOCK_MONOTONIC does not seem available, so clock_gettime is out. I've read in some places that mach_absolute_time() might be the right way to go, but after reading that it was a 'cpu dependent value', it instantly made me wonder if it is using rtdsc underneath. Thus, the value could drift over time even if it is monotonic. Also, issues with thread affinity could result in meaningfully different results from calling the function (making it not monotonic across all cores). Of course, that is

Stopwatch vs. using System.DateTime.Now for timing events [duplicate]

你。 提交于 2019-11-26 11:49:58
This question already has an answer here: Is DateTime.Now the best way to measure a function's performance? 15 answers I wanted to track the performance of my code so I stored the start and end time using System.DateTime.Now . I took the difference between the two as the time my code to execute. I noticed though that the difference didn't appear to be accurate. So I tried using a Stopwatch object. This turned out to be much, much more accurate. Can anyone tell me why Stopwatch would be more accurate than calculating the difference between a start and end time using System.DateTime.Now ? BTW, I