标签: microbenchmark

使用Caliper时如何指定命令行?

我发现谷歌的微基准项目Caliper非常有趣,但文档仍然(除了一些例子)完全不存在.

我有两种不同的情况需要影响Jiper Caliper的命令行启动:

  1. 我需要设置一些固定的(理想情况下在几个固定值之间交替)-D参数
  2. 我需要指定一些固定的(理想情况下在几个固定值之间交替)JVM参数

我看到一些关于添加这样的功能的讨论,但我不能断定它是否已被添加,在这种情况下语法变成了什么?

一些示例或指向Java文档的指针(假设这在某处都有记录)等将非常感谢!

java benchmarking microbenchmark caliper

3
推荐指数
1
解决办法
1386
查看次数

Java的.连接字符串.微基准

在第一步,我运行此代码:

public class Demo  {
    public static void main(String[] args) {
        String x = "x";
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++)

        {

            x = x.concat("s");

            // x+="k";

        }

        System.out.println(System.currentTimeMillis() - start);
    }

}
Run Code Online (Sandbox Code Playgroud)

出:13579.

在第二步,我运行此代码:

public class Demo {
    public static void main(String[] args) {
        String x = "x";
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++)

        {

            //x = x.concat("s");

             x+="k";

        }

        System.out.println(System.currentTimeMillis() - start);
    }

} …
Run Code Online (Sandbox Code Playgroud)

java string microbenchmark

3
推荐指数
1
解决办法
336
查看次数

Java Microbenchmarking Harness vs System.getNanotime()

问题1:为什么JMH比简单更好System.getNanotime()

问题2:从结果(看看benchmarking results部分)我可以得出什么结果validateLongKeyBinary比64%更快validateLongKeyAscii

示例(代码):

import net.spy.memcached.util.StringUtils;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;

public class KeyBench {

private static final String LONG_KEY = "thisIsAFunkyKeyWith_underscores_AndAlso334" +
            "3252545345NumberslthisIsAFunkyKeyWith_underscores_AndAlso3343252545345Numbe" +
            "rslthisIsAFunkyKeyWith_underscores_AndAlso3343252545345NumberslthisIsAFunkyK" +
            "eyWith_underscores_AndAlso3343252545345Numbersl";


    @GenerateMicroBenchmark
    public void validateLongKeyBinary() {
        StringUtils.validateKey(LONG_KEY, true);
    }

    @GenerateMicroBenchmark
    public void validateLongKeyAscii() {
        StringUtils.validateKey(LONG_KEY, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

基准测试结果

# Running: benchmarks.KeyBench.validateLongKeyAscii

Result : 393,667 ±(95%) 13,985 ±(99%) 20,094 ops/ms
  Statistics: (min, avg, max) = (357,445, 393,667, 413,004), stdev = 19,552
  Confidence intervals: 95% …
Run Code Online (Sandbox Code Playgroud)

java benchmarking microbenchmark jmh

3
推荐指数
1
解决办法
864
查看次数

Rubinius和JRuby怎么可能这么慢?

我决定看看迭代一系列哈希需要多长时间.以下是代码:

pairs = [{name: "firstname", value: "string"},{name: "lastname", value: "string"},{name: "country", value: "string"},{name: "city", value: "string"},{name: "state", value: "string"},{name: "company", value: "string"},{name: "year", value: "string"},{name: "political_affiliation", value: "string"},{name: "social_security_number", value: "string"}] * 1000
blank = {}

start = Time.now
pairs.each do |pair|
  blank[pair[:name]] = pair[:value]
end

p Time.now - start
Run Code Online (Sandbox Code Playgroud)

通过从循环之前的当前时间减去循环之后的当前时间来计算时间.

根据代码中的数学计算,这是YARV 2.1.1中计算所花费的时间:

0.001962017
Run Code Online (Sandbox Code Playgroud)

这是Rubinius 2.2.6花了多长时间:

0.022598
Run Code Online (Sandbox Code Playgroud)

和jRuby 1.7.12

0.022317
Run Code Online (Sandbox Code Playgroud)

据说Rubinius和jRuby比YARV具有性能优势.为什么他们花费的时间几乎是执行相同基本操作的12倍?这是正常的还是我有不正确的配置?

ruby benchmarking rubinius jruby microbenchmark

3
推荐指数
1
解决办法
1742
查看次数

试图对lambda性能进行基准测试

我读过这篇文章:Java 8 lambdas和匿名内部类之间的性能差异并提供了文章

它说:

Lambda调用的行为与匿名类调用完全相同

"好的"我说并决定编写我自己的基准测试,我已经使用过jmh,这里是下面的(我还添加了方法参考的基准).

public class MyBenchmark {

    public static final int TESTS_COUNT = 100_000_000;

    @Benchmark
    public void testMethod_lambda() {
        X x = i -> test(i);
        for (long i = 0; i < TESTS_COUNT; i++) {
            x.x(i);
        }
    }
    @Benchmark
    public void testMethod_methodRefernce() {
        X x = this::test;
        for (long i = 0; i < TESTS_COUNT; i++) {
            x.x(i);
        }
    }
    @Benchmark
    public void testMethod_anonymous() {
        X x = new X() {
            @Override …
Run Code Online (Sandbox Code Playgroud)

java performance lambda microbenchmark java-8

3
推荐指数
2
解决办法
485
查看次数

为什么lambda IntStream.anyMatch()比天真的实现慢10?

我最近在分析我的代码并发现了一个有趣的瓶颈.这是基准:

@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
public class Contains {

    private int[] ar = new int[] {1,2,3,4,5,6,7};

    private int val = 5;

    @Benchmark
    public boolean naive() {
        return contains(ar, val);
    }

    @Benchmark
    public boolean lambdaArrayStreamContains() {
        return Arrays.stream(ar).anyMatch(i -> i == val);
    }

    @Benchmark
    public boolean lambdaIntStreamContains() {
        return IntStream.of(ar).anyMatch(i -> i == val);
    }

    private static boolean contains(int[] ar, int value) {
        for …
Run Code Online (Sandbox Code Playgroud)

java performance lambda microbenchmark

3
推荐指数
1
解决办法
937
查看次数

在通过类变量调用时,Java 9中的Class.cast和Class.isInstance方法实际上是内在的吗?

我一直在寻找通过Java的9个新特性的方法和发现,现在的方法Class.cast 是内在的与太一起Class.isInstance.

我做了简单的基准来检查:

@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, batchSize = 1000)
@Measurement(iterations = 20, time = 1, batchSize = 1000)
public class Casting {

    public Object msg;
    public Class<String> type;

    @Setup
    public void setup(BenchmarkParams params) {
        type = String.class;
        msg = "123";
    }

    @Benchmark
    public boolean isInstanceMethod() {
        return type.isInstance(msg);
    }

    @Benchmark
    public boolean isInstanceMethodExplicit() {
        return String.class.isInstance(msg);
    }

    @Benchmark
    public boolean isInstanceRegular() {
        return msg instanceof String;
    }

    @Benchmark
    public String …
Run Code Online (Sandbox Code Playgroud)

java performance microbenchmark java-9

3
推荐指数
1
解决办法
175
查看次数

为什么.toString()似乎修复了StringBuilder的OutOfMemoryError异常?

我正在学习如何使用JMH对事物进行微观标记.我开始看似简单的东西:StringBuildervs的字符串连接String +=.

根据我的理解,我应该创建一个State包含一个实例的对象,StringBuilder因为我不想对它的构造函数进行基准测试(我也不想每次迭代都是空的).同样适用于String +=测试 - 我希望我的String对象State与新字符串连接.

这是我的代码:

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class Test {

    @State(Scope.Thread)
    public static class BenchmarkState {

        public StringBuilder    builder;
        public String           regularString;

        @Setup(Level.Iteration)
        public void setup() {
            builder         = new StringBuilder();
            regularString   = "";
        }

    }

    @Benchmark
    public String stringTest(BenchmarkState state) {
        state.regularString += "hello";
        return state.regularString;
    }

    @Benchmark
    public String stringBuilderTest(BenchmarkState state) {
        state.builder.append("hello");
        return state.builder.toString();
    }

    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java benchmarking microbenchmark jmh

3
推荐指数
1
解决办法
321
查看次数

如何启动两个CPU内核以同时运行指令?

例如,在X86中,两个CPU内核运行不同的软件线程。
同时,这两个线程需要同时在其CPU内核上运行。
是否有办法同步这2个CPU内核/线程,或类似的方法以使它们(几乎)同时(在指令级别)开始运行?

linux x86-64 linux-kernel microbenchmark thread-synchronization

3
推荐指数
1
解决办法
96
查看次数

为什么迭代std :: array比迭代std :: vector快得多?

编者注:
启用优化的后续问题仅对循环
计时为什么通过`std :: vector`进行迭代比通过`std :: array`进行迭代更快?
在这里我们可以看到延迟分配页面错误在读取未初始化的BSS内存与在定时循环之外初始化的动态分配+写入内存的影响。


我尝试分析此代码:

#include <vector>
#include <array>
#include <stdio.h>

using namespace std;

constexpr int n = 400'000'000;
//vector<int> v(n);
array<int, n> v;

int main()
{
    int res = 0;
    for(int x : v)
        res += x;
    printf("%d\n", res);
}
Run Code Online (Sandbox Code Playgroud)

在我的机器上,array版本比快vector

在这种情况下,内存分配是无关紧要的,因为它只有一次。

$ g++ arrVsVec.cpp -O3
$ time ./a.out
0

real    0m0,445s
user    0m0,203s
sys 0m0,238s
Run Code Online (Sandbox Code Playgroud)
$ g++ arrVsVec.cpp -O3
$ time ./a.out
0

real    0m0,749s
user    0m0,273s
sys …
Run Code Online (Sandbox Code Playgroud)

c++ linux performance microbenchmark

3
推荐指数
3
解决办法
185
查看次数