forked from deltixlab/DFP
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from epam/master
Add isRounded and isRoundedToReciprocal functions, unit tests and benchmarks
- Loading branch information
Showing
8 changed files
with
655 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
java/dfp/src/jmh/java/com/epam/deltix/dfp/ExceptionCatchBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.epam.deltix.dfp; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
import java.math.RoundingMode; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Warmup(time = 3, iterations = 1) | ||
@Measurement(time = 3, iterations = 3) | ||
@State(Scope.Thread) | ||
public class ExceptionCatchBenchmark { | ||
private long value; | ||
private int n; | ||
private int r; | ||
|
||
@Setup | ||
public void setUp() { | ||
value = Decimal64Utils.parse("0.9999999999999999"); | ||
n = 3; | ||
r = 1000; | ||
} | ||
|
||
private static boolean isRoundedToTenPower(final long value, final int n) { | ||
try { | ||
Decimal64Utils.round(value, n, RoundingMode.UNNECESSARY); | ||
return true; | ||
} | ||
catch (final ArithmeticException e) { | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean isRoundedToReciprocal(final long value, final int r) { | ||
try { | ||
Decimal64Utils.roundToReciprocal(value, r, RoundingMode.UNNECESSARY); | ||
return true; | ||
} | ||
catch (final ArithmeticException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void isRoundedToTenPower(Blackhole bh) { | ||
bh.consume(Decimal64Utils.isRounded(value, n)); | ||
} | ||
|
||
@Benchmark | ||
public void isRoundedToTenPowerCatch(Blackhole bh) { | ||
bh.consume(isRoundedToTenPower(value, n)); | ||
} | ||
|
||
@Benchmark | ||
public void isRoundedToReciprocal(Blackhole bh) { | ||
bh.consume(Decimal64Utils.isRoundedToReciprocal(value, r)); | ||
} | ||
|
||
@Benchmark | ||
public void isRoundedToReciprocalCatch(Blackhole bh) { | ||
bh.consume(isRoundedToReciprocal(value, r)); | ||
} | ||
} |
Oops, something went wrong.