Which is fastest? SELECT SQL_CALC_FOUND_ROWS FROM `table`, or SELECT COUNT(*)

It depends. See the MySQL Performance Blog post on this subject: To SQL_CALC_FOUND_ROWS or not to SQL_CALC_FOUND_ROWS? Just a quick summary: Peter says that it depends on your indexes and other factors. Many of the comments to the post seem to say that SQL_CALC_FOUND_ROWS is almost always slower – sometimes up to 10x slower – … Read more

Java Reflection Performance

Yes – absolutely. Looking up a class via reflection is, by magnitude, more expensive. Quoting Java’s documentation on reflection: Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which … Read more

What is the effect of ordering if…else if statements by probability?

As a general rule, most if not all Intel CPUs assume forward branches are not taken the first time they see them. See Godbolt’s work. After that, the branch goes into a branch prediction cache, and past behavior is used to inform future branch prediction. So in a tight loop, the effect of misordering is … Read more

Why is early return slower than else?

This is a pure guess, and I haven’t figured out an easy way to check whether it is right, but I have a theory for you. I tried your code and get the same of results, without_else() is repeatedly slightly slower than with_else(): >>> T(lambda : without_else()).repeat() [0.42015745017874906, 0.3188967452567226, 0.31984281521812363] >>> T(lambda : with_else()).repeat() [0.36009842032996175, … Read more

What is the difference between Unary Plus/Number(x) and parseFloat(x)?

The difference between parseFloat and Number parseFloat/parseInt is for parsing a string, while Number/+ is for coercing a value to a number. They behave differently. But first let’s look at where they behave the same: parseFloat(‘3’); // => 3 Number(‘3’); // => 3 parseFloat(‘1.501’); // => 1.501 Number(‘1.501’); // => 1.501 parseFloat(‘1e10’); // => 10000000000 … Read more

MySQL OR vs IN performance

I needed to know this for sure, so I benchmarked both methods. I consistenly found IN to be much faster than using OR. Do not believe people who give their “opinion”, science is all about testing and evidence. I ran a loop of 1000x the equivalent queries (for consistency, I used sql_no_cache): IN: 2.34969592094s OR: … Read more

Measuring execution time of a function in C++

It is a very easy-to-use method in C++11. You have to use std::chrono::high_resolution_clock from <chrono> header. Use it like so: #include <chrono> /* Only needed for the sake of this example. */ #include <iostream> #include <thread> void long_operation() { /* Simulating a long, heavy operation. */ using namespace std::chrono_literals; std::this_thread::sleep_for(150ms); } int main() { using … Read more