#Loop Benchmarking

16 messages · Page 1 of 1 (latest)

sleek lynx
#

Anyone know why the 2nd one is faster than the 1st one? I'm using JMH and I've also tried returning a value for the process methods to see if there is a different result due to dead code elimination or something of the sorts. I'm not quite understanding what the issue is.

    public void process() {
        for (int i = 0; i < size; i++) {
            process(ids[i]);
        }
    }
    public void process2() {
        int[] ids = new int[size];
        for (int i = 0; i < size; i++) {
            ids[i] = this.ids[i];
            process(ids[i]);
        }
    }```
stiff shardBOT
#

This post has been reserved for your question.

Hey @sleek lynx! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

paper summit
#

the 2nd one is not faster than the first one

#

the first one has 199.5 ops per second, the second one has 152.9

sleek lynx
paper summit
#

can you show the entire benchmark output

#

also what does process look like

#

the method taking an int

sleek lynx
#
# Run progress: 0.00% complete, ETA 00:00:20
# Fork: 1 of 1
# Warmup Iteration   1: 263.584 ops/s
# Warmup Iteration   2: 637.922 ops/s
# Warmup Iteration   3: 796.244 ops/s
# Warmup Iteration   4: 820.141 ops/s
# Warmup Iteration   5: 817.004 ops/s
Iteration   1: 709.704 ops/s
Iteration   2: 671.150 ops/s
Iteration   3: 771.217 ops/s
Iteration   4: 805.927 ops/s
Iteration   5: 815.270 ops/s


Result "benchmarks.ECSBenchmark.process":
  754.654 �(99.9%) 240.249 ops/s [Average]
  (min, avg, max) = (671.150, 754.654, 815.270), stdev = 62.392
  CI (99.9%): [514.405, 994.902] (assumes normal distribution)

# Run progress: 50.00% complete, ETA 00:00:11
# Fork: 1 of 1
# Warmup Iteration   1: 1157.714 ops/s
# Warmup Iteration   2: 1304.912 ops/s
# Warmup Iteration   3: 1316.117 ops/s
# Warmup Iteration   4: 1333.961 ops/s
# Warmup Iteration   5: 1272.252 ops/s
Iteration   1: 1300.704 ops/s
Iteration   2: 1284.038 ops/s
Iteration   3: 1341.037 ops/s
Iteration   4: 1319.669 ops/s
Iteration   5: 1298.296 ops/s


Result "benchmarks.ECSBenchmark.process2":
  1308.749 �(99.9%) 84.941 ops/s [Average]
  (min, avg, max) = (1284.038, 1308.749, 1341.037), stdev = 22.059
  CI (99.9%): [1223.808, 1393.689] (assumes normal distribution)


# Run complete. Total time: 00:00:22

Benchmark               Mode  Cnt     Score     Error  Units
ECSBenchmark.process   thrpt    5   754.654 � 240.249  ops/s
ECSBenchmark.process2  thrpt    5  1308.749 �  84.941  ops/s```
#
    // ECS class
    public boolean process() {
        for (int systemID = 0; systemID < systems.length; systemID++) {
            systems[systemID].process();
        }
        return true;
    }
    // ECS class
    public boolean process2() {
        for (int systemID = 0; systemID < systems.length; systemID++) {
            systems[systemID].process2();
        }
        return true;
    }

    // EntitySystem class
    public boolean process() {
        for (int i = 0; i < size; i++) {
            process(ids[i]);
        }
        return true;
    }

    // EntitySystem class
    public boolean process2() {
        int[] ids = new int[size];
        for (int i = 0; i < size; i++) {
            ids[i] = this.ids[i];
            process(ids[i]);
        }
        return true;
    }```
#

I've got a class called ECS and another called EntitySystem

The benchmark methods are here:

    @Benchmark
    public boolean process(Blackhole blackhole) {
        return ecs.process();
    }

    @Benchmark
    public boolean process2(Blackhole blackhole) {
        return ecs.process2();
    }```
#

Oh my apologies, I missed what you asked about the process method

#
this.ecs = new ECS(new infrastructure.ecsprototype.EntitySystem(TestComponent.class) {

            TypeArray<TestComponent> typeArr;

            @Override
            public void process(int entityID) {
                TestComponent component = typeArr.get(entityID);
                component.x = 2;
            }
        });

Even if I clear the process(int) method, the process2 method is slightly faster

#

Considering there are more allocations and instructions in the process2 method, I should just know without a doubt that it shouldn't be faster and it could just be a jvm optimization that is acting weird. I'll go ahead and close the thread. I'm sure something will pop up in the future explaining why weird things like this happen

stiff shardBOT