Questions tagged [microbenchmark]

A microbenchmark attempts to measure the performance of a "small" bit of code. These tests are typically in the sub-millisecond range. The code being tested usually performs no I/O, or else is a test of some single, specific I/O task.

Microbenchmarking is very different from profiling! When profiling, you work with an entire application, either in production or in an environment very painstakingly contrived to resemble production. Because of this, you get performance data that is, for lack of a better term, real. When you microbenchmark, you get a result that is essentially fictional, and you must be very careful about what conclusions you draw from it.

Still, for either type always apply the old adage:
Premature optimization is the root of all evil.

485 questions
971
votes
11 answers

How do I write a correct micro-benchmark in Java?

How do you write (and run) a correct micro-benchmark in Java? I'm looking for some code samples and comments illustrating various things to think about. Example: Should the benchmark measure time/iteration or iterations/time, and why? Related: Is…
John Nilsson
  • 17,001
  • 8
  • 32
  • 42
434
votes
6 answers

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

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…
89
votes
6 answers

If statement vs if-else statement, which is faster?

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…
Julien__
  • 1,962
  • 1
  • 15
  • 25
89
votes
6 answers

What is microbenchmarking?

I've heard this term used, but I'm not entirely sure what it means, so: What DOES it mean and what DOESN'T it mean? What are some examples of what IS and ISN'T microbenchmarking? What are the dangers of microbenchmarking and how do you avoid…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
75
votes
11 answers

How can I find the missing value more concisely?

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…
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
73
votes
7 answers

In Java, can & be faster than &&?

In this code: if (value >= x && value <= y) { when value >= x and value <= y are as likely true as false with no particular pattern, would using the & operator be faster than using &&? Specifically, I am thinking about how && lazily evaluates the…
55
votes
4 answers

Hidden performance cost in Scala?

I came across this old question and did the following experiment with scala 2.10.3. I rewrote the Scala version to use explicit tail recursion: import scala.annotation.tailrec object ScalaMain { private val t = 20 private def run() { var i…
Phil
  • 5,595
  • 5
  • 35
  • 55
54
votes
0 answers

Java 11 -- performance regressions against Java 8?

UPDATE: Seeing as each method might be suffering from a different performance issue I decided to split this question into two: Empty methods noticeably slower in Java 11 than Java 8 Consuming stack traces noticeably slower in Java 11 than Java…
Gili
  • 86,244
  • 97
  • 390
  • 689
51
votes
4 answers

Getting an accurate execution time in C++ (micro seconds)

I want to get an accurate execution time in micro seconds of my program implemented with C++. I have tried to get the execution time with clock_t but it's not accurate. (Note that micro-benchmarking is hard. An accurate timer is only a small part…
user3323616
  • 513
  • 1
  • 4
  • 4
48
votes
1 answer

Why is the StringBuilder chaining pattern sb.append(x).append(y) faster than regular sb.append(x); sb.append(y)?

I have a microbenchmark that shows very strange results: @BenchmarkMode(Mode.Throughput) @Fork(1) @State(Scope.Thread) @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000) @Measurement(iterations = 40, time = 1, timeUnit…
Dmitriy Dumanskiy
  • 11,657
  • 9
  • 37
  • 57
41
votes
3 answers

Difference between MATLAB's numel and length functions

I know that length(x) returns max(size(x)) and numel(x) returns the total number of elements of x, but which is better for a 1 by n array? Does it matter, or are they interchangeable in this case? EDIT: Just for kicks: Looks like they're the same…
Doresoom
  • 7,398
  • 14
  • 47
  • 61
30
votes
1 answer

What does OpenJDK JMH "score error" exactly mean?

I am using http://openjdk.java.net/projects/code-tools/jmh/ for benchmarking and i get a result like: Benchmark Mode Samples Score Score error Units o.a.f.c.j.b.TestClass.test1 avgt 5 2372870,600 …
salyh
  • 2,095
  • 1
  • 17
  • 31
25
votes
4 answers

Why is summing an array of value types slower then summing an array of reference types?

I'm trying to understand better how memory works in .NET, so I'm playing with BenchmarkDotNet and diagnozers. I've created a benchmark comparing class and struct performance by summing array items. I expected that summing value types will always be…
23
votes
2 answers

Why are two separate loops faster than one?

I want to understand what kind of optimizations Java does to consecutive for loops. More precisely, I'm trying to check if loop fusion is performed. Theoretically, I was expecting that this optimization was not done automatically and was expecting…
22
votes
1 answer

Why is String concatenation faster than String.valueOf for converting an Integer to a String?

I have a benchmark : @BenchmarkMode(Mode.Throughput) @Fork(1) @State(Scope.Thread) @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000) @Measurement(iterations = 40, time = 1, timeUnit = TimeUnit.SECONDS, batchSize =…
Dmitriy Dumanskiy
  • 11,657
  • 9
  • 37
  • 57
1
2 3
32 33