microbenchmark

Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

一笑奈何 提交于 2019-12-03 01:30:47
问题 I'm writing some code in Java where, at some point, the flow of the program is determined by whether two int variables, "a" and "b", are non-zero (note: a and b are never negative, and never within integer overflow range). I can evaluate it with if (a != 0 && b != 0) { /* Some code */ } Or alternatively if (a*b != 0) { /* Some code */ } Because I expect that piece of code to run millions of times per run, I was wondering which one would be faster. I did the experiment by comparing them on a

If statement vs if-else statement, which is faster? [closed]

本秂侑毒 提交于 2019-12-03 00:58:51
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I argued with a friend the other day about those two snippets. Which is faster and why ? value = 5; if (condition) { value = 6; } and: if (condition) { value = 6; } else { value = 5; } What if value is a matrix ? Note: I know that value = condition ? 6 : 5; exists and I expect it

Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

北战南征 提交于 2019-12-02 13:47:32
I'm writing some code in Java where, at some point, the flow of the program is determined by whether two int variables, "a" and "b", are non-zero (note: a and b are never negative, and never within integer overflow range). I can evaluate it with if (a != 0 && b != 0) { /* Some code */ } Or alternatively if (a*b != 0) { /* Some code */ } Because I expect that piece of code to run millions of times per run, I was wondering which one would be faster. I did the experiment by comparing them on a huge randomly generated array, and I was also curious to see how the sparsity of the array (fraction of

Check of microbenchmark results fails with data.table changed by reference

喜夏-厌秋 提交于 2019-12-01 21:19:00
There are some answers on SO where timings are compared without checking the results. However, I prefer to see whether an expression is correct and fast. The microbenchmark package supports this with the check parameter. Unfortunately, the check fails on expressions which change a data.table by reference , i.e., the check does not recognize that results are different. Case 1: data.table expressions where check works as expected library(data.table) library(microbenchmark) # minimal data.table 1 col, 3 rows dt <- data.table(x = c(1, 1, 10)) # define check function as in example section of help

Using SIMD on amd64, when is it better to use more instructions vs. loading from memory?

≡放荡痞女 提交于 2019-12-01 15:21:24
I have some highly perf sensitive code. A SIMD implementation using SSEn and AVX uses about 30 instructions, while a version that uses a 4096 byte lookup table uses about 8 instructions. In a microbenchmark, the lookup table is faster by 40%. If I microbenchmark, trying to invalidate the cache very 100 iterations, they appear about the same. In my real program, it appears that the non-loading version is faster, but it's really hard to get a provably good measurement, and I've had measurements go both ways. I'm just wondering if there are some good ways to think about which one would be better

Using SIMD on amd64, when is it better to use more instructions vs. loading from memory?

…衆ロ難τιáo~ 提交于 2019-12-01 15:02:25
问题 I have some highly perf sensitive code. A SIMD implementation using SSEn and AVX uses about 30 instructions, while a version that uses a 4096 byte lookup table uses about 8 instructions. In a microbenchmark, the lookup table is faster by 40%. If I microbenchmark, trying to invalidate the cache very 100 iterations, they appear about the same. In my real program, it appears that the non-loading version is faster, but it's really hard to get a provably good measurement, and I've had measurements

Bring code into the L1 instruction cache without executing it

霸气de小男生 提交于 2019-12-01 12:31:23
Let's say I have a function that I plan to execute as part of a benchmark. I want to bring this code into the L1 instruction cache prior to executing since I don't want to measure the cost of I$ misses as part of the benchmark. The obvious way to do this is to simply execute the code at least once before the benchmark, hence "warming it up" and bringing it into the L1 instruction cache and possibly the uop cache, etc. What are my alternatives in the case I don't want to execute the code (e.g., because I want the various predictors which key off of instruction addresses to be cold)? One

Bring code into the L1 instruction cache without executing it

风流意气都作罢 提交于 2019-12-01 10:13:24
问题 Let's say I have a function that I plan to execute as part of a benchmark. I want to bring this code into the L1 instruction cache prior to executing since I don't want to measure the cost of I$ misses as part of the benchmark. The obvious way to do this is to simply execute the code at least once before the benchmark, hence "warming it up" and bringing it into the L1 instruction cache and possibly the uop cache, etc. What are my alternatives in the case I don't want to execute the code (e.g.

Benchmarking code - am I doing it right?

青春壹個敷衍的年華 提交于 2019-12-01 05:51:48
问题 I want to benchmark a C/C++ code. I want to measure cpu time, wall time and cycles/byte. I wrote some mesurement functions but have a problem with cycles/byte. To get a cpu time I wrote a function getrusage() with RUSAGE_SELF , for wall time i use clock_gettime with MONOTONIC , to get cycles/byte I use rdtsc . I process an input buffer of size, for example, 1024: char buffer[1024] . How do I benchmark: Do a warm-up phase, simply call fun2measure(args) 1000 times: for(int i=0; i<1000; i++)

How can I find the missing value more concisely?

两盒软妹~` 提交于 2019-11-29 20:22:32
The following code checks if x and y are distinct values (the variables x , y , z can only have values a , b , or c ) and if so, sets z to the third character: if x == 'a' and y == 'b' or x == 'b' and y == 'a': z = 'c' elif x == 'b' and y == 'c' or x == 'c' and y == 'b': z = 'a' elif x == 'a' and y == 'c' or x == 'c' and y == 'a': z = 'b' Is is possible to do this in a more, concise, readable and efficient way? z = (set(("a", "b", "c")) - set((x, y))).pop() I am assuming that one of the three cases in your code holds. If this is the case, the set set(("a", "b", "c")) - set((x, y)) will consist