# Time it

### concepts about time and performance

CPU bound vs IO bound

A program is CPU bound if it would go faster if the CPU were faster, i.e. it spends the majority of its time simply using the CPU (doing calculations). A program that computes new digits of π will typically be CPU-bound, it’s just crunching numbers.

A program is I/O bound if it would go faster if the I/O subsystem was faster. Which exact I/O system is meant can vary; I typically associate it with disk, but of course networking or communication in general is common too. A program that looks through a huge file for some data might become I/O bound, since the bottleneck is then the reading of the data from disk (actually, this example is perhaps kind of old-fashioned these days with hundreds of MB/s coming in from SSDs).

the goal of the optimizaiton is to change the CPU bound program into the IO bound program.

real time（wall time） vs user time vs sys time

Elapsed real time, real time, wall-clock time, or wall time is the actual time taken from the start of a computer program to the end (wall time 与 real time实际代表同样的含义). on linux，there is a command called time，use time <executable>可以得到sys，user，real time。一般说的cpu time 更多指的是cpu所花在这个程序上的时间，这里要注意用词的准确性，有几次把cpu time与wall time给弄混了。

real time就是实际的时间，比如一个program从1:00开始运行，到2:00结束，这个process的real time就是1h，但是在这个1h的时间中，可能这个程序并没有一直在cpu上运行，比如可能是0.5h在cpu上运行，另外0.5小时在sleep，这个时候就cpu time实际上就不是1h。user time 表示在用户态所花的时间，sys time 表示cpu在内核状态下所花费的时间。比如下面这个case：

real time 花费掉了1s，然后user和sys很少，并且user和sys的总和也远小于real，这说明虽然程序从开始运行到结束是1s，但cpu的占用率在这个程序运行的这段时间还是比较低(0.003s in total)。

### c中的timer方式

python 中的timer就和strightforware

### timer for distributed components

time 提供了两种方式 一种是tick record，每发一次请求记录一次时间，另一种是time span的形式，只记录开始和结束的时间。