Calculate Your Code Performance
Tags: javascript, beginners, cpp, programming
Published: 2021-10-23 15:40:36

Benchmarking your code is a very important step to maintaining good code. It does not particularly matter whether the language is "fast" or "slow" as each language has its target platform where it needs to do well.

## JavaScript Benchmarking Code
In JavaScript there is a really simple way to measure the performance of your code and can be rally useful to test easily on the client side of your web browser. 

Let's look at an example:
```js
function reallyExpensiveFunction() {
  for (let i = 0; i < 10000; ++i) {
    console.log("Hi\n");
  }
}

console.time('reallyExpensiveFunction');
console.timeEnd('reallyExpensiveFunction');
```
We can bench our functions by using the function `console.time` to start and `console.timeEnd` to end our bench.

### Here is an output you might get
![JS Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q24zgb90j7ew0z32sequ.png)
 

You can try this example on [repl-it](https://replit.com/@aboss123/JSBench#index.js).


## C Benchmarking Code
Believe it or not, the same code in C is very similar to the JavaScript example.

Let's look at this example:
```c
#include <stdio.h>
#include <time.h>

void really_expensive_function() {
  for (int i = 0; i < 10000; ++i) {
    printf("Hi\n");
  }
}

int main() {
  clock_t start = clock();
  really_expensive_function();
  clock_t end   = clock();
  printf("Took %f seconds\n", (((float)(end-start) / CLOCKS_PER_SEC)));
  return 0;
}

```
`clock_t` is a typedef for `long` on my machine and is likely the same for yours. Despite that, you should still use `clock_t` as it may be different on different machines. We get the system time before and after the really expensive function and are able to get the amount of time in seconds.

You can try this example on [repl-it](https://replit.com/@aboss123/CBenchmark#main.c).

### Here is an output you might get
![C Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym78tdqiwfiopw8ffqer.png)

## Understanding the Reality
The examples are great for testing out small pieces of code but are not feasible for large code bases where complex benchmarking is necessary. 

## Complex Benchmarking
* Profiling
  * What does a profiler *actually* do? A program profile gives the developer the ability to be able to measure both space and time complexity of their functions in their program. This is particularly important if your program has a major bottleneck causing slowdowns, which is especially disastrous if it is a system where many requests are made. An example of such a tool is [orbit](https://github.com/google/orbit) which can visualize the performance points in your program.
* Benchmarking IO Operations
  * IO operations are ones that take in user input or read or write to system files, mainly requiring operations from the operating system kernel. These operations are usually the most expensive operations in your program. However, since time spent in system calls is not manageable by the programmer, it is best to reduce the amount of system calls that are made to improve performance.
* Distributed Systems
  * These systems are complicated and so it is necessary to make sure that the performance of the system is in check. In general this is because each computer is not entirely the same, and so it becomes difficult in order to assess performance accurately. Different computers have different CPUs, network sockets, and configurations and these computers interact with routers and other network systems that communicate with each other that impact the way performance is calculated. It is best to determine the performance of such systems in a way that gives **relative** benchmark, or a benchmark that is good enough for a team working on it to be able to assess the program.


## Resources

**JavaScript**:
For JavaScript, there are already some good tools for benchmarking, most notable being [Benchmark.js](https://github.com/bestiejs/benchmark.js/) and [Bench-Rest](https://github.com/jeffbski/bench-rest). Using these tools will allow you to be able to properly test the performance of your code. It is generally given that you want to use software already tested for acceptable benchmarking as the demos shown today are often trivial and may not give all the results you want.


**C++**:
C++ has quite a number of benchmarking libraries some of the recent ones involving C++ 20's flexibility. The most notable being [Google Bench](https://github.com/google/benchmark) and [UT](https://github.com/boost-ext/ut). C does not have many specific benchmarking libraries, but you can easily integrate C code with C++ benchmarking libraries in order to test the performance of your C code.


## Conclusion
In the end it's up to you how you choose to benchmark your code. Generally, you want to code your project before you benchmark it and if performance is really a concern then you can opt to use these benchmarking libraries or use a performance profile to find bottlenecks. I hope you learned something today :).