# 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

### c++中的timer方式

Another way in cpp to add the timer:

It seems there is not much difference between the steady_clock and the high_resolution_clock.

### The timer in parallel sitution

In general case, when we need to time the parallel execution, we just need to print out the timer for leader process, such as using rank=0 to control it. It is unnecessary to print out the time spent on each specific process. If we need to time each specific process, writting the performance data to each file separately into parallel file system (this is a common operation for the data check pointing things) might be a more efficient way then writting all information into a one single file.

One issue for the parallel I/O is that, some specific process may need comparatively long time to open a file, such as the description in this article.

### Timer for distributed components

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

### Performance model

Timing 的一个重要的意义就是构建 performance model 或者了解当前这个service的质量是如何的。有时候在想一个research是不是interests可能是要看看这research当中使用了多少python。换句话说就是使用了多少数学工具或者相关的算法。

### Take care of the error and uncertainty

Always trying to ask what are associated errors when time sth or using this timer for specific performance model. (Type of the error, bouond of the error, source of the error etc.). Figuring out these things can give you a better understanding regarding the model. The error bar is always needed to describe the figure. Testing one group of results or doing one experiments are not enough, you need to get enough results to show the uncertainty. The error is always a good aspects to discuss the quality of the model, or how to move forward based on the current model approach.