Calculate Your Code Performance

Ashish Bailkeri - Oct 23 '21 - - Dev Community

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:



function reallyExpensiveFunction() {
  for (let i = 0; i < 10000; ++i) {
    console.log("Hi\n");
  }
}

console.time('reallyExpensiveFunction');
console.timeEnd('reallyExpensiveFunction');


Enter fullscreen mode Exit fullscreen mode

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

You can try this example on repl-it.

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:



#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;
}



Enter fullscreen mode Exit fullscreen mode

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.

Here is an output you might get

C Output

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 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 and 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 and 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 :).

. . . . . .