Last active
May 3, 2023 04:07
-
-
Save feilongjiang/b59bdd8db8460242bafac4a2ee6c2e06 to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. | |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
* | |
* This code is free software; you can redistribute it and/or modify it | |
* under the terms of the GNU General Public License version 2 only, as | |
* published by the Free Software Foundation. | |
* | |
* This code is distributed in the hope that it will be useful, but WITHOUT | |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
* version 2 for more details (a copy is included in the LICENSE file that | |
* accompanied this code). | |
* | |
* You should have received a copy of the GNU General Public License version | |
* 2 along with this work; if not, write to the Free Software Foundation, | |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
* | |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
* or visit www.oracle.com if you need additional information or have any | |
* questions. | |
*/ | |
package org.openjdk.bench.java.math; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.*; | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@State(Scope.Thread) | |
@Warmup(iterations = 5, time = 1) | |
@Measurement(iterations = 5, time = 1) | |
@Fork(value = 3) | |
public class FloatConversion { | |
@Param({"2048"}) | |
public int size; | |
public float[] farr; | |
public double[] darr; | |
public int[] f2i; | |
public long[] f2l; | |
public int[] d2i; | |
public long[] d2l; | |
@Setup(Level.Trial) | |
public void BmSetup() { | |
int i = 0; | |
Random r = new Random(1024); | |
farr = new float[size]; | |
darr = new double[size]; | |
f2i = new int[size]; | |
f2l = new long[size]; | |
d2i = new int[size]; | |
d2l = new long[size]; | |
for (; i < size - 50; i++) { | |
farr[i] = r.nextFloat(); | |
darr[i] = r.nextDouble(); | |
} | |
// Fill with INF and NaN | |
for (; i < size; i++) { | |
if (i % 3 == 0) { | |
farr[i] = Float.NaN; | |
darr[i] = Double.NaN; | |
} else if (i % 3 == 1) { | |
farr[i] = Float.POSITIVE_INFINITY; | |
darr[i] = Double.POSITIVE_INFINITY; | |
} else { | |
farr[i] = Float.NEGATIVE_INFINITY; | |
darr[i] = Double.NEGATIVE_INFINITY; | |
} | |
} | |
} | |
@Benchmark | |
public int[] floatToInt() { | |
for (int i = 0; i < farr.length; i++) { | |
f2i[i] = (int) farr[i]; | |
} | |
return f2i; | |
} | |
@Benchmark | |
public long[] floatToLong() { | |
for (int i = 0; i < farr.length; i++) { | |
f2l[i] = (long) farr[i]; | |
} | |
return f2l; | |
} | |
@Benchmark | |
public int[] doubleToInt() { | |
for (int i = 0; i < darr.length; i++) { | |
d2i[i] = (int) darr[i]; | |
} | |
return d2i; | |
} | |
@Benchmark | |
public long[] doubleToLong() { | |
for (int i = 0; i < darr.length; i++) { | |
d2l[i] = (long) darr[i]; | |
} | |
return d2l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment