我发现谷歌的微基准项目Caliper非常有趣,但文档仍然(除了一些例子)完全不存在.
我有两种不同的情况需要影响Jiper Caliper的命令行启动:
我看到一些关于添加这样的功能的讨论,但我不能断定它是否已被添加,在这种情况下语法变成了什么?
一些示例或指向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) 问题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) 我决定看看迭代一系列哈希需要多长时间.以下是代码:
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倍?这是正常的还是我有不正确的配置?
我读过这篇文章: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) 我最近在分析我的代码并发现了一个有趣的瓶颈.这是基准:
@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的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) 我正在学习如何使用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) 例如,在X86中,两个CPU内核运行不同的软件线程。
同时,这两个线程需要同时在其CPU内核上运行。
是否有办法同步这2个CPU内核/线程,或类似的方法以使它们(几乎)同时(在指令级别)开始运行?
linux x86-64 linux-kernel microbenchmark thread-synchronization
编者注:
启用优化的后续问题仅对循环
计时:为什么通过`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) microbenchmark ×10
java ×7
benchmarking ×4
performance ×4
jmh ×2
lambda ×2
linux ×2
c++ ×1
caliper ×1
java-8 ×1
java-9 ×1
jruby ×1
linux-kernel ×1
rubinius ×1
ruby ×1
string ×1
x86-64 ×1