microbenchmark

Speed of if compared to conditional

孤街浪徒 提交于 2019-12-07 19:18:44
问题 I had the idea I would turn some of my if blocks into single lines, using the conditional operator. However, I was wondering if there would be a speed discrepancy. I ran the following test: static long startTime; static long elapsedTime; static String s; public static void main(String[] args) { startTime = System.nanoTime(); s = ""; for (int i= 0; i < 1000000000; i++) { if (s.equals("")) { s = ""; } } elapsedTime = System.nanoTime() - startTime; System.out.println("Type 1 took this long: " +

Why is iterating an std::array much faster than iterating an std::vector?

蓝咒 提交于 2019-12-07 12:49:46
问题 Editor's note: Followup question with optimization enabled that times only the loop : Why is iterating though `std::vector` faster than iterating though `std::array`? where we can see the effect of lazy-allocation page faults in reading uninitialized BSS memory vs. dynamically-allocated + written memory that's initialized outside the timed loop. I tried profiling this code: #include <vector> #include <array> #include <stdio.h> using namespace std; constexpr int n = 400'000'000; //vector<int>

Why is the first run always much slower?

只谈情不闲聊 提交于 2019-12-07 12:03:52
问题 I wrote a macro that reports the time required to run a given operation. It runs it a number of times and prints out the time for each run in nanoseconds. The first run always takes significantly more time than subsequent ones. Why is that so? Here are the results of 10 x 10 runs, timing Thread.yield() : > (dotimes [x 10] (prn (times 10 (Thread/yield)))) [55395 1659 622 561 591 702 795 719 742 624] [3255 772 884 677 787 634 605 664 629 657] [3431 789 965 671 774 767 627 627 521 717] [2653 780

Do java caches results of the methods

别等时光非礼了梦想. 提交于 2019-12-07 04:56:59
问题 I use JMH to specify the complexity of the operation. If you've never worked with JMH, don't worry. JMH will just launch the estimateOperation method multiple times and then get the average time. Question: [narrow] will this program calculate Math.cbrt(Integer.MAX_VALUE) each time? Or it just calculate it once and return cached result afterwards? @GenerateMicroBenchmark public void estimateOperation() { calculate(); } public int calculate() { return Math.cbrt(Integer.MAX_VALUE); } Question:

shade for parameter resource: Cannot find 'resource' in class org.apache.maven.plugins.shade.resource.ManifestResourceTransformer

别来无恙 提交于 2019-12-06 22:11:52
问题 I'm working on a maven project. I'm trying to integrate jmh benchmarking into my project. The pom.xml of my maven project... <parent> <groupId>platform</groupId> <artifactId>platform-root</artifactId> <version>3.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> <artifactId>platform-migration</artifactId> <packaging>jar</packaging> <name>Platform Migration</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compile.source>1.7<

On improving Haskell's performance compared to C in fibonacci micro-benchmark

谁说我不能喝 提交于 2019-12-06 17:56:19
问题 I came across this question, which compared the performance of various compilers on computing fibonaci numbers the naive way. I tried doing this with Haskell to see how it compares to C. C code: #include <stdio.h> #include <stdlib.h> int fib (int n) { if (n < 2) return 1; return fib (n-1) + fib (n-2); } int main (int argc, char* argv[]) { printf ("%i\n", fib (atoi(argv[1]))); return 0; } Result: > gcc -O3 main.c -o fib > time ./fib 40 165580141 real 0m0.421s user 0m0.420s sys 0m0.000s Haskell

Simple micro-benchmark with JMH

久未见 提交于 2019-12-06 08:04:50
问题 Inspired by another question on Stack Overflow, I have written a micro-benchmark to check, what is more efficient: conditionally checking for zero divisor or catching and handling an ArithmeticException Below is my code: @State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class MyBenchmark { private int a = 10; // a little bit less obvious than int b = 0; private int b = (int) Math.floor(Math.random()); @Benchmark public float conditional() { if

How can I use JMH for Scala benchmarks together with sbt?

六月ゝ 毕业季﹏ 提交于 2019-12-05 16:07:37
问题 I have tried to use jmh together with sbt, but so far I have not managed to set it up properly so that .scala based benchmarks work. As the combination sbt + .java based benchmarks works, I tried to start from that base. I am using sbt 0.13.1. .java based benchmarks using sbt build.sbt import AssemblyKeys._ name := "scala-benchmark" version := "1.0" scalaVersion := "2.10.3" scalacOptions += "-deprecation" libraryDependencies += "org.openjdk.jmh" % "jmh-core" % "0.5.5" libraryDependencies +=

Measure precision of timer (e.g. Stopwatch/QueryPerformanceCounter)

做~自己de王妃 提交于 2019-12-05 15:26:38
Given that the Stopwatch class in C# can use something like three different timers underneath e.g. System timer e.g. precision of approx +-10 ms depending on timer resolution that can be set with timeBeginPeriod it can be approx +-1 ms . Time Stamp Counter (TSC) e.g. with a tick frequency of 2.5MHz or 1 tick = 400 ns so ideally a precision of that. High Precision Event Timer (HPET) e.g. with a tick frequency of 25MHz or 1 tick = 40 ns so ideally a precision of that. how can we measure the observable precision of this? Precision being defined as Precision refers to the closeness of two or more

Why is the first run always much slower?

喜欢而已 提交于 2019-12-05 13:04:23
I wrote a macro that reports the time required to run a given operation. It runs it a number of times and prints out the time for each run in nanoseconds. The first run always takes significantly more time than subsequent ones. Why is that so? Here are the results of 10 x 10 runs, timing Thread.yield() : > (dotimes [x 10] (prn (times 10 (Thread/yield)))) [55395 1659 622 561 591 702 795 719 742 624] [3255 772 884 677 787 634 605 664 629 657] [3431 789 965 671 774 767 627 627 521 717] [2653 780 619 632 616 614 606 602 629 667] [2373 759 700 676 557 639 659 654 659 676] [2884 929 627 604 689 614