Pull to refresh
0
Send message
Конкретно в этом примере Java (1.05 сек) у меня медленнее C (0.2 сек) в 5 раз, при использовании буфера в мегабайт.
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        if (args.length < 1) {
            System.out.println("Usage main <filename>");
            System.exit(1);
        }

        byte[] buf = new byte[1024*1024];
        FileInputStream is = new FileInputStream(args[0]);
        int readed;
        byte total = 0;
        while ((readed = is.read(buf, 0, buf.length)) > 0) {
            for (int i = 0; i < readed; i++) {
                total += buf[i];
            }
        }

        System.out.println("Result is:" + total);
    }
}

и даже версия для JMH выдает аналогичные результаты
import org.openjdk.jmh.annotations.*;
import java.io.*;

public class MyBenchmark {

    @Benchmark
    @BenchmarkMode({Mode.AverageTime})
    public byte testMethod() throws Exception{
        // This is a demo/sample template for building your JMH benchmarks. Edit as needed.
        // Put your benchmark code here.
        byte[] buf = new byte[1024*1024];
        FileInputStream is = new FileInputStream("/tmp/test");
        int readed;
        byte total = 0;
        while ((readed = is.read(buf, 0, buf.length)) > 0) {
            total += sumBuf(buf, readed);
        }
        return total;
    }

    public byte sumBuf(byte[] buf, int readed) {
        byte total=0;
        for (int i = 0; i < readed; i++) {
            total += buf[i];
        }
        return total;
    }

}

при этом для в C нет ручных оптимизаций, а только -O2

Information

Rating
Does not participate
Registered
Activity