Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LANG-1601] Refine performance of fraction.pow #611

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/main/java/org/apache/commons/lang3/math/Fraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,49 @@ public Fraction abs() {
* {@code Integer.MAX_VALUE}
*/
public Fraction pow(final int power) {
if (power == 1) {
return this;
}
if (power == 0) {
return ONE;
}
if (power == -1) {
return this.invert();
}
final Fraction base = this.reduce();
if (base.denominator == 0) {
if (power > 0) {
throw new ArithmeticException("The denominator must not be zero");
} else {
return ZERO;
}
}
if (base.numerator == 0) {
if (power < 0) {
throw new ArithmeticException("The denominator must not be zero");
} else {
return ZERO;
}
}
if (power > 0) {
return new Fraction(
pow(base.numerator, power),
pow(base.denominator, power)
);
}
if (power == Integer.MIN_VALUE) {
return new Fraction(
pow(this.denominator, Integer.MAX_VALUE) * this.denominator,
pow(this.numerator, Integer.MAX_VALUE) * this.numerator
);
}
return new Fraction(
pow(base.denominator, -power),
pow(base.numerator, -power)
);
}

public Fraction powOld(final int power) {
if (power == 1) {
return this;
} else if (power == 0) {
Expand All @@ -552,6 +595,36 @@ public Fraction pow(final int power) {
}
}

/**
* Raise an int to an int power.
* Notice that this function is copied and modified directly from class ArithmeticUtils in commons-numbers-core.
*
* @param k Number to raise.
* @param e Exponent (must be positive or zero).
* @return \( k^e \)
* @throws ArithmeticException if the result would overflow.
*/
private static int pow(final int k,
final int e) {
int exp = e;
int result = 1;
int k2p = k;
while (true) {
if ((exp & 0x1) != 0) {
result = Math.multiplyExact(result, k2p);
}

exp >>= 1;
if (exp == 0) {
break;
}

k2p = Math.multiplyExact(k2p, k2p);
}

return result;
}

/**
* <p>Gets the greatest common divisor of the absolute value of
* two numbers, using the "binary gcd" method which avoids
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.math;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, jvmArgs = {"-server", "-Xms2048M", "-Xms2048M"})
public class FractionPowPerformanceTest {
private Random random = new Random(0);
private int[] a = buildInts(1000, 8);
private int[] b = buildInts(1000, 8);
private int[] c = buildInts(1000, 8);

private int[] buildInts(int length, int bound) {
int[] res = new int[length];
for (int i = 0; i < length; i++) {
res[i] = random.nextInt(bound);
}
return res;
}

@Benchmark
public Fraction[] testNew() {
final int length = a.length;
Fraction[] res = new Fraction[length];
for (int i = 0; i < length; i++) {
res[i] = Fraction.getFraction(a[i], b[i] + 1).pow(c[i]);
}
return res;
}

@Benchmark
public Fraction[] testOld() {
final int length = a.length;
Fraction[] res = new Fraction[length];
for (int i = 0; i < length; i++) {
res[i] = Fraction.getFraction(a[i], b[i] + 1).powOld(c[i]);
}
return res;
}

@Test
public void testEquals() {
final int length = a.length;
for (int i = 0; i < length; i++) {
Assertions.assertEquals(
Fraction.getFraction(a[i], b[i] + 1).powOld(c[i]),
Fraction.getFraction(a[i], b[i] + 1).pow(c[i])
);
}
}
}