Skip to content

Instantly share code, notes, and snippets.

@FranciscoRibeiro
Last active February 26, 2018 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FranciscoRibeiro/2d3928761f76e4f7cecfcfcdf7fc96d5 to your computer and use it in GitHub Desktop.
Save FranciscoRibeiro/2d3928761f76e4f7cecfcfcdf7fc96d5 to your computer and use it in GitHub Desktop.
JMH Benchmark: two loops vs one loop
package loopfusion;
public class Capsule {
private int number;
private String word;
public Capsule(int number, String word) {
this.number = number;
this.word = word;
}
public int getNumber() {
return number;
}
public String getWord() {
return word;
}
public void setNumber(int number) {
this.number = number;
}
public void setWord(String word) {
this.word = word;
}
@Override
public String toString() {
return "{" + number +
", " + word + '}';
}
}
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package loopfusion;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.concurrent.TimeUnit;
@Fork(value = 1)
@Warmup(iterations = 10, time = 60)
@Measurement(iterations = 10, time = 60)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.SECONDS)
public class MyBenchmark {
@State(Scope.Thread)
public static class ListState{
public List<Capsule> caps = new ArrayList<>();
@Setup(Level.Trial)
public void setup(){
Random r = new Random(3);
for(int n = 0; n < 20000000; n++){
int randomN = r.nextInt();
Capsule c = new Capsule(randomN, "word" + randomN);
caps.add(c);
}
}
@TearDown(Level.Trial)
public void tearDown(){
caps.clear();
}
}
@Benchmark
public List<String> multipleLoops(ListState state) {
List<Capsule> intermediate = new ArrayList<>();
List<String> res = new ArrayList<>();
int totalLength = 0;
for (Capsule c : state.caps) {
if(c.getNumber() > 100000000){
intermediate.add(c);
}
}
for (Capsule c : intermediate) {
String s = "new_word" + c.getNumber();
res.add(s);
}
for(String s : res){
totalLength += s.length();
}
System.out.println(totalLength);
return res;
}
@Benchmark
public List<String> singleLoop(ListState state){
List<String> res = new ArrayList<>();
int totalLength = 0;
for (Capsule c : state.caps) {
if(c.getNumber() > 100000000){
String s = "new_word" + c.getNumber();
res.add(s);
}
}
for(String s : res){
totalLength += s.length();
}
System.out.println(totalLength);
return res;
}
}
// Benchmark Mode Cnt Score Error Units
// MyBenchmark.multipleLoops avgt 10 7.397 ± 0.242 s/op
// MyBenchmark.singleLoop avgt 10 8.092 ± 0.058 s/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment