所以我想对一些基本的java功能进行基准测试,以便为这个问题添加一些信息:将方法声明为静态会带来什么好处.
我知道写作基准有时并不容易,但这里发生的事情我无法解释.
请注意,我并没有考虑如何解决这个问题,而是为什么会发生这种情况*
测试类:
public class TestPerformanceOfStaticVsDynamicCalls {
private static final long RUNS = 1_000_000_000L;
public static void main( String [] args ){
new TestPerformanceOfStaticVsDynamicCalls().run();
}
private void run(){
long r=0;
long start, end;
for( int loop = 0; loop<10; loop++ ){
// Benchmark
start = System.currentTimeMillis();
for( long i = 0; i < RUNS; i++ ) {
r += addStatic( 1, i );
}
end = System.currentTimeMillis();
System.out.println( "Static: " + ( end - start ) + …Run Code Online (Sandbox Code Playgroud) 我已经使用了这个基准测试java8-lambda-performance-test,在运行它时我做了以下几点:
1.Disabled内在用法
2.Disabled Inlining
3.Disabled编译模式
我发现禁用两个第一次优化对结果没有影响.
这很奇怪,而且当运行基准和打印内在时,我没有找到任何对内在的调用 compiledLambdaForm
由于数学内在函数大量使用_min,_pow ...我期待禁用内在函数会降低性能
[简答:糟糕的基准测试方法.你觉得我现在已经想到了.]
问题表现为"找到一种快速计算x ^ y的方法,其中x和y是正整数".典型的"快速"算法如下所示:
public long fastPower(int x, int y) {
// Replaced my code with the "better" version described below,
// but this version isn't measurably faster than what I had before
long base = x; // otherwise, we may overflow at x *= x.
long result = y % 2 == 1 ? x : 1;
while (y > 1) {
base *= base;
y >>= 1;
if (y % 2 == 1) result *= base;
}
return …Run Code Online (Sandbox Code Playgroud) 我用java for循环进行了一些运行时测试,并发现了一个奇怪的行为.对于我的代码,我需要原始类型的包装器对象,如int,double等,以模拟io和输出参数,但这不是重点.只需看我的代码.具有字段访问权限的对象如何比原始类型更快?
for 具有prtimitive类型的循环:
public static void main(String[] args) {
double max = 1000;
for (int j = 1; j < 8; j++) {
double i;
max = max * 10;
long start = System.nanoTime();
for (i = 0; i < max; i++) {
}
long end = System.nanoTime();
long microseconds = (end - start) / 1000;
System.out.println("MicroTime primitive(max: ="+max + "): " + microseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
MicroTime原语(max:= 10000.0):110
MicroTime原语(max:= 100000.0):1081
MicroTime原语(max:=
1000000.0 ):2450 MicroTime原语(max:= 1.0E7):28248
MicroTime原语(max:= 1.0E8) :276205 …
我创建了这个类是不可变的,并且具有流畅的API:
public final class Message {
public final String email;
public final String escalationEmail;
public final String assignee;
public final String conversationId;
public final String subject;
public final String userId;
public Message(String email, String escalationEmail, String assignee, String conversationId, String subject, String userId) {
this.email = email;
this.escalationEmail = escalationEmail;
this.assignee = assignee;
this.conversationId = conversationId;
this.subject = subject;
this.userId = userId;
}
public Message() {
email = "";
escalationEmail = "";
assignee = "";
conversationId = "";
subject = …Run Code Online (Sandbox Code Playgroud) import java.util.Random;
public class Test{
static int r = new Random().nextInt(2);
static int a(){
return r==1 ? 1 :0;
}
public static void test1() throws Exception {
//
System.out.println(1403187139018L);
for (int i = 0; i < 1073741824; i++) {}//*
// Thread.sleep(20000);
long d = 0;
for (int j = 0; j < 10; j++) {
long y = System.currentTimeMillis();
for (int x = 0; x < 1073741823; x++) {
d += r==0?1:0;
}
System.out.println((System.currentTimeMillis() -y));
}
}
public static void …Run Code Online (Sandbox Code Playgroud) java ×7
jvm ×3
performance ×3
benchmarking ×2
jit ×2
algorithm ×1
fluent ×1
for-loop ×1
immutability ×1
javacompiler ×1
jvm-hotspot ×1
lambda ×1
optimization ×1
profile ×1