java - What is microbenchmarking? -


I used this word, but I'm not sure what that means, so:

  • What does this mean and does not mean?
  • What are some examples of what is not microbainchmarkmarking?
  • What are the risks of microbanmarkmarking and how do you avoid it?
    • (Or is this a good thing?)

This means what it can say on the tin - it measures the performance of some" small ", like the operating system kernel to the system call.

The danger is that in order to direct optimization, people can use the results obtained from microbankmarking. And as we all know:

We should forget about the small potential, 97% of the time say: Optimization of time is the root of all evils "- Donald Knuth

There are several factors that obliterate the results of microbankmarks. Compiler optimization is one of them. If the measured operation takes so little time that whatever you use to measure Taking longer than actual operation For example, your microbainchmark

For example, a can take a microbain mark of the overhead for the loops:

  zero TestForLoop () {time For start = gate time (); (int i = 0; i & lt; 1000000000; ++ i) {} time has passed = gate time () - start; time passed; reflection = beta / 1000000000; printf (" Time has passed for each instance:% d \ n ", passed repatriation);}  

Obviously compilers can see that the loop is exactly Not and does not generate any code to loop all.

zero TestForLoop (Uc) {int sum = 0; Start time = GetTime (); For (Int i = 0; I <1000000000; ++ i) {++ sum; } Time has passed = GetTime () - Start; Time has passed # Reversed = 1000000000; Printf ("time has passed for each walk:% d \ n", elapsed priority); }

The compiler can see that the variable sum can not be used for anything and it is not optimizing, and also for this Can be able to optimize the loop. but wait! If we do this:

  zero from TestForLoop () {int sum = 0; Start time = GetTime (); For (Int i = 0; I <1000000000; ++ i) {++ sum; } Time has passed = GetTime () - Start; Time has passed # Reversed = 1000000000; Printf ("The time elapsed for each recurrence:% d \ n", the rendering past); Printf ("Yoga:% d \ n", Yoga); // added}  

The compiler might be smart enough to understand that sum will always be a constant value, and all of these are also remotely customized Will do

Zero TestFileOpenPerformance () {FILE * File that many people will be surprised these days on optimization capabilities of the compiler

= Null; Start time = GetTime (); For (int i = 0; i <1000000000; ++ i) {file = fopen ("testfile.dat"); Fclose (file); } Time has passed = GetTime () - Start; Time has passed # Reversed = 1000000000; Printf ("The time has passed for each file:% d \ n", the rendered parity); }

Even this is not a useful test! The operating system can see that the file is being open several times, so it can preload it in memory to improve performance. A lot of all operating systems do this. The same thing happens when you open the application - the operating system can find the top ~ 5 applications that you open most and the application code is already in memory while booting into the computer Load!

In fact, there are countless variables that fall into placements: areas of context (such as array vs. linked lists), the effect of cache and memory bandwidth, compiler inlineing, compiler implementation, compiler switch, number of processor cores , Optimization at the processor level, operating system scheduler, operating system background processes, etc.

Therefore microbatchchanking is not a very useful metric in many cases. It certainly does not replace the standard of the entire program with well-defined test cases (profiling). Write the first readable code, then to see the profile of what should be done, if any.

I would like to emphasize that microbacchmark is not bad per copy , but anyone has to use it carefully to them (for many other things related to computers This is correct)


Comments