├── .gitignore ├── LICENSE ├── README.md ├── deepdive ├── Bitset │ ├── Bitset.java │ └── README.md └── Mysterious │ ├── Mysterious.java │ ├── README.md │ └── amd64asm.txt ├── pom.xml └── src └── main └── java └── me └── lemire ├── hashing └── InterleavedHash.java └── microbenchmarks ├── algorithms ├── MersenneTwisterFast.java └── Shuffle.java ├── arrays └── ArraysBench.java ├── bitset └── Bitset.java ├── bytebuffer └── DirectVSHeapVSArray.java ├── integersum ├── Array.java ├── BasicSummer.java ├── FastSummer.java ├── FixedSummer.java ├── NaiveArray.java ├── ReverseBasicSummer.java ├── ReverseFastSummer.java ├── ReverseFixedSummer.java ├── ReverseSmartSummer.java ├── SillySummer.java ├── SmartSummer.java └── SumBenchmark.java ├── mysteries ├── MysteriousLambda.java ├── MysteriousLambdaJustCopy.java └── StringMerge.java ├── random ├── Division.java └── RandomNumberGenerator.java └── rangequery └── RangeMinimum.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | /target 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | microbenchmarks 2 | =============== 3 | 4 | Range Minimum Query 5 | -------------------- 6 | 7 | http://stackoverflow.com/questions/42570135/java-method-call-performance 8 | 9 | mvn clean install 10 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.rangequery.RangeMinimum 11 | 12 | 13 | 14 | Mysterious issue 15 | ------------------- 16 | 17 | http://stackoverflow.com/questions/41622613/why-is-my-java-lambda-with-a-dummy-assignment-much-faster-than-without-it 18 | 19 | mvn clean install 20 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.mysteries.MysteriousLambda 21 | 22 | Bitset 23 | --------- 24 | mvn clean install 25 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.bitset.Bitset 26 | 27 | Compare direct vs. heap buffers 28 | ------------------------------- 29 | 30 | 31 | mvn clean install 32 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.bytebuffer.DirectVSHeapVSArray 33 | 34 | 35 | 36 | Java RNG is slow 37 | ----------------- 38 | 39 | mvn clean install 40 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.random.RandomNumberGenerator 41 | 42 | 43 | Integer sums 44 | -------------- 45 | 46 | This is my answer to the following blog post: http://www.vitavonni.de/blog/201412/2014122201-java-sum-of-array-comparisons.html 47 | 48 | mvn clean install 49 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.integersum.SumBenchmark -wi 5 -i 5 -f 1 50 | 51 | 52 | 53 | Hashing 54 | --------- 55 | 56 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me/lemire/hashing/InterleavedHash 57 | 58 | 59 | Shuffle 60 | ----------- 61 | java -cp target/microbenchmarks-0.0.1-jar-with-dependencies.jar me.lemire.microbenchmarks.algorithms.Shuffle 62 | 63 | -------------------------------------------------------------------------------- /deepdive/Bitset/Bitset.java: -------------------------------------------------------------------------------- 1 | 2 | public class Bitset { 3 | 4 | public static void xor(int[] a, int[] b) { 5 | final int length = a.length < b.length ? a.length : b.length; 6 | for( int k = 0; k < length ; ++k ) { 7 | a[k] = b[k]; 8 | } 9 | } 10 | 11 | public static long sum(int[] a) { 12 | long x = 0; 13 | for(int i=0; i < a.length; i++) x+= a[i]; 14 | return x; 15 | } 16 | public static void crap(int[] a, int[] b) { 17 | for (int ibeg=0; ibeg fooList = new ArrayList(); 10 | 11 | public BenchmarkState() { 12 | for (int ctr = 0; ctr < 1000; ++ctr) { 13 | fooList.add(new Foo().alpha("a" + ctr).beta("b" + ctr)); 14 | } 15 | } 16 | 17 | } 18 | 19 | public static String merge(String x1, String x2) { 20 | int len1 = x1.length(); 21 | int len2 = x2.length(); 22 | char[] buffer = new char[len1 + len2]; 23 | x1.getChars(0, len1, buffer, 0); 24 | x2.getChars(0, len2, buffer, len1); 25 | return new String(buffer,0,len1 + len2); 26 | } 27 | 28 | public FooPrime[] basicstream(BenchmarkState s) { 29 | return (FooPrime[]) s.fooList.stream().map(it -> { 30 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 31 | }).toArray(FooPrime[]::new); 32 | } 33 | 34 | public FooPrime[] mergebasicstream(BenchmarkState s) { 35 | return (FooPrime[]) s.fooList.stream().map(it -> { 36 | return new FooPrime().gamma(merge(it.getAlpha(), it.getBeta())); 37 | }).toArray(FooPrime[]::new); 38 | } 39 | 40 | 41 | public FooPrime[] basicstreamstringbuilder(BenchmarkState s) { 42 | return (FooPrime[]) s.fooList.stream().map(it -> { 43 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 44 | }).toArray(FooPrime[]::new); 45 | } 46 | 47 | public FooPrime[] tweakedbasicstreamstringbuilder(BenchmarkState s) { 48 | return (FooPrime[]) s.fooList.stream().map(it -> { 49 | int stuff = it.getAlpha().length(); 50 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 51 | }).toArray(FooPrime[]::new); 52 | } 53 | 54 | public FooPrime[] nullbasicstreamstringbuilder(BenchmarkState s) { 55 | return (FooPrime[]) s.fooList.stream().map(it -> { 56 | if( it.getAlpha() == null) throw new NullPointerException(); 57 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 58 | }).toArray(FooPrime[]::new); 59 | } 60 | 61 | 62 | 63 | 64 | 65 | public FooPrime[] tweakedbasicstream(BenchmarkState s) { 66 | return (FooPrime[]) s.fooList.stream().map(it -> { 67 | int stuff = it.getAlpha().length(); 68 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 69 | }).toArray(FooPrime[]::new); 70 | } 71 | 72 | public FooPrime[] nullbasicstream(BenchmarkState s) { 73 | return (FooPrime[]) s.fooList.stream().map(it -> { 74 | if( it.getAlpha() == null) throw new NullPointerException(); 75 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 76 | }).toArray(FooPrime[]::new); 77 | } 78 | 79 | 80 | public FooPrime[] betanullbasicstream(BenchmarkState s) { 81 | return (FooPrime[]) s.fooList.stream().map(it -> { 82 | if( it.getBeta() == null) throw new NullPointerException(); 83 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 84 | }).toArray(FooPrime[]::new); 85 | } 86 | 87 | 88 | public static void main(String[] args) { 89 | for (int k = 0; k < 10000; k++) { 90 | BenchmarkState bs = new BenchmarkState(); 91 | Mysterious ml = new Mysterious(); 92 | ml.nullbasicstream(bs); 93 | //System.out.println(ml.basicstream(bs)); 94 | } 95 | } 96 | 97 | } 98 | 99 | final class Foo { 100 | private String alpha; 101 | private String beta; 102 | 103 | public String getAlpha() { 104 | return alpha; 105 | } 106 | 107 | public String getBeta() { 108 | return beta; 109 | } 110 | 111 | public void setAlpha(String alpha) { 112 | this.alpha = alpha; 113 | } 114 | 115 | public void setBeta(String beta) { 116 | this.beta = beta; 117 | } 118 | 119 | public Foo alpha(String alpha) { 120 | this.alpha = alpha; 121 | return this; 122 | } 123 | 124 | public Foo beta(String beta) { 125 | this.beta = beta; 126 | return this; 127 | } 128 | } 129 | 130 | final class FooPrime { 131 | private String gamma; 132 | 133 | public String getGamma() { 134 | return gamma; 135 | } 136 | 137 | public void setGamma(String gamma) { 138 | this.gamma = gamma; 139 | } 140 | 141 | public FooPrime gamma(String gamma) { 142 | this.gamma = gamma; 143 | return this; 144 | } 145 | 146 | @Override 147 | public int hashCode() { 148 | final int prime = 31; 149 | int result = 1; 150 | result = prime * result + ((gamma == null) ? 0 : gamma.hashCode()); 151 | return result; 152 | } 153 | 154 | @Override 155 | public boolean equals(Object obj) { 156 | if (this == obj) 157 | return true; 158 | if (obj == null) 159 | return false; 160 | if (getClass() != obj.getClass()) 161 | return false; 162 | FooPrime other = (FooPrime) obj; 163 | if (gamma == null) { 164 | if (other.gamma != null) 165 | return false; 166 | } else if (!gamma.equals(other.gamma)) 167 | return false; 168 | return true; 169 | } 170 | 171 | @Override 172 | public String toString() { 173 | return "FooPrime [gamma=" + gamma + "]"; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /deepdive/Mysterious/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Grab the appropriate library from https://kenai.com/projects/base-hsdis/downloads and install in /usr/lib (Linux). 4 | 5 | Run: 6 | 7 | ``` 8 | javac Mysterious.java 9 | java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly Mysterious > myst.txt 10 | ``` 11 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | me.lemire.microbenchmarks 4 | microbenchmarks 5 | 0.0.1 6 | 7 | 8 | 1.4 9 | UTF-8 10 | benchmarks 11 | 12 | jar 13 | 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-compiler-plugin 19 | 2.3.2 20 | 21 | 1.8 22 | 1.8 23 | 24 | 25 | 26 | maven-assembly-plugin 27 | 28 | 29 | package 30 | 31 | single 32 | 33 | 34 | 35 | 36 | 37 | jar-with-dependencies 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.github.alexeyr.pcg 48 | pcg 49 | 0.9.1 50 | 51 | 52 | org.openjdk.jmh 53 | jmh-core 54 | ${jmh.version} 55 | 56 | 57 | org.openjdk.jmh 58 | jmh-generator-annprocess 59 | ${jmh.version} 60 | provided 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/hashing/InterleavedHash.java: -------------------------------------------------------------------------------- 1 | package me.lemire.hashing; 2 | 3 | import org.openjdk.jmh.annotations.Benchmark; 4 | import org.openjdk.jmh.annotations.BenchmarkMode; 5 | import org.openjdk.jmh.annotations.Mode; 6 | import org.openjdk.jmh.annotations.OutputTimeUnit; 7 | import org.openjdk.jmh.annotations.Scope; 8 | import org.openjdk.jmh.annotations.State; 9 | import org.openjdk.jmh.runner.Runner; 10 | import org.openjdk.jmh.runner.RunnerException; 11 | import org.openjdk.jmh.runner.options.Options; 12 | import org.openjdk.jmh.runner.options.OptionsBuilder; 13 | 14 | import java.nio.ByteBuffer; 15 | import java.util.concurrent.TimeUnit; 16 | import java.util.*; 17 | 18 | @State(Scope.Thread) 19 | @BenchmarkMode(Mode.AverageTime) 20 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 21 | public class InterleavedHash { 22 | 23 | @Benchmark 24 | public int standardHash(BenchmarkState s) { 25 | char[] val = s.array; 26 | int len = val.length; 27 | int h = 1; 28 | for (int i = 0; i < len; i++) { 29 | h = 31 * h + val[i]; 30 | } 31 | return h; 32 | 33 | } 34 | 35 | 36 | @Benchmark 37 | public int hashCode(BenchmarkState s) { 38 | return Arrays.hashCode(s.array); 39 | } 40 | 41 | @Benchmark 42 | public int standardHash4(BenchmarkState s) { 43 | char[] val = s.array; 44 | int len = val.length; 45 | int h = 1; 46 | int i = 0; 47 | for (; i + 3 < len; i += 4) { 48 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * val[i] + 31 * 31 49 | * val[i + 1] + 31 * val[i + 2] + val[i + 3]; 50 | } 51 | for (; i < len; i++) { 52 | h = 31 * h + val[i]; 53 | } 54 | 55 | return h; 56 | 57 | } 58 | 59 | @Benchmark 60 | public int standardHash8(BenchmarkState s) { 61 | char[] val = s.array; 62 | int len = val.length; 63 | int h = 1; 64 | int i = 0; 65 | for (; i + 7 < len; i += 8) { 66 | h = 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * 31 67 | * 31 * 31 * 31 * val[i] + 31 * 31 * 31 * 31 * 31 * 31 68 | * val[i + 1] + 31 * 31 * 31 * 31 * 31 * val[i + 2] + 31 69 | * 31 * 31 * 31 * val[i + 3] + 31 * 31 * 31 * val[i + 4] 70 | + 31 * 31 * val[i + 5] + 31 * val[i + 6] + val[i + 7]; 71 | } 72 | 73 | for (; i + 3 < len; i += 4) { 74 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * val[i] + 31 * 31 75 | * val[i + 1] + 31 * val[i + 2] + val[i + 3]; 76 | } 77 | for (; i < len; i++) { 78 | h = 31 * h + val[i]; 79 | } 80 | 81 | return h; 82 | 83 | } 84 | 85 | @Benchmark 86 | public int standardHashByte(BenchmarkState s) { 87 | byte[] val = s.barray; 88 | int len = val.length; 89 | int h = 1; 90 | for (int i = 0; i < len; i++) { 91 | h = 31 * h + val[i]; 92 | } 93 | return h; 94 | 95 | } 96 | 97 | 98 | @Benchmark 99 | public int hashCodeByte(BenchmarkState s) { 100 | return Arrays.hashCode(s.barray); 101 | } 102 | 103 | @Benchmark 104 | public int standardHashByte4(BenchmarkState s) { 105 | byte[] val = s.barray; 106 | int len = val.length; 107 | int h = 1; 108 | int i = 0; 109 | for (; i + 3 < len; i += 4) { 110 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * val[i] + 31 * 31 111 | * val[i + 1] + 31 * val[i + 2] + val[i + 3]; 112 | } 113 | for (; i < len; i++) { 114 | h = 31 * h + val[i]; 115 | } 116 | 117 | return h; 118 | 119 | } 120 | 121 | @Benchmark 122 | public int standardHashByte8(BenchmarkState s) { 123 | byte[] val = s.barray; 124 | int len = val.length; 125 | int h = 1; 126 | int i = 0; 127 | for (; i + 7 < len; i += 8) { 128 | h = 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * 31 129 | * 31 * 31 * 31 * val[i] + 31 * 31 * 31 * 31 * 31 * 31 130 | * val[i + 1] + 31 * 31 * 31 * 31 * 31 * val[i + 2] + 31 131 | * 31 * 31 * 31 * val[i + 3] + 31 * 31 * 31 * val[i + 4] 132 | + 31 * 31 * val[i + 5] + 31 * val[i + 6] + val[i + 7]; 133 | } 134 | 135 | for (; i + 3 < len; i += 4) { 136 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * val[i] + 31 * 31 137 | * val[i + 1] + 31 * val[i + 2] + val[i + 3]; 138 | } 139 | for (; i < len; i++) { 140 | h = 31 * h + val[i]; 141 | } 142 | 143 | return h; 144 | 145 | } 146 | 147 | @Benchmark 148 | public int standardHashByteBuffer(BenchmarkState s) { 149 | ByteBuffer val = s.bb; 150 | int len = val.limit(); 151 | int h = 1; 152 | for (int i = 0; i < len; i++) { 153 | byte b = val.get(i); 154 | h = 31 * h + b; 155 | } 156 | return h; 157 | 158 | } 159 | 160 | 161 | @Benchmark 162 | public int standardHashByteBuffer4(BenchmarkState s) { 163 | ByteBuffer val = s.bb; 164 | int len = val.limit(); 165 | int h = 1; 166 | int i = 0; 167 | for (; i + 3 < len; i += 4) { 168 | byte b1 = val.get(i); 169 | byte b2 = val.get(i+1); 170 | byte b3 = val.get(i+2); 171 | byte b4 = val.get(i+3); 172 | 173 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * b1 + 31 * 31 174 | * b2 + 31 * b3 + b4; 175 | } 176 | for (; i < len; i++) { 177 | byte b = val.get(i); 178 | h = 31 * h + b; 179 | } 180 | 181 | return h; 182 | 183 | } 184 | 185 | @Benchmark 186 | public int standardHashByteBuffer8(BenchmarkState s) { 187 | ByteBuffer val = s.bb; 188 | int len = val.limit(); 189 | int h = 1; 190 | int i = 0; 191 | for (; i + 7 < len; i += 8) { 192 | byte b1 = val.get(i); 193 | byte b2 = val.get(i+1); 194 | byte b3 = val.get(i+2); 195 | byte b4 = val.get(i+3); 196 | byte b5 = val.get(i+4); 197 | byte b6 = val.get(i+5); 198 | byte b7 = val.get(i+6); 199 | byte b8 = val.get(i+7); 200 | h = 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * 31 201 | * 31 * 31 * 31 * b1 + 31 * 31 * 31 * 31 * 31 * 31 202 | * b2 + 31 * 31 * 31 * 31 * 31 * b3 + 31 203 | * 31 * 31 * 31 * b4 + 31 * 31 * 31 * b5 204 | + 31 * 31 * b6 + 31 * b7 + b8; 205 | } 206 | 207 | for (; i + 3 < len; i += 4) { 208 | byte b1 = val.get(i); 209 | byte b2 = val.get(i+1); 210 | byte b3 = val.get(i+2); 211 | byte b4 = val.get(i+3); 212 | h = 31 * 31 * 31 * 31 * h + 31 * 31 * 31 * b1 + 31 * 31 213 | * b2 + 31 * b3 + b4; 214 | } 215 | for (; i < len; i++) { 216 | byte b = val.get(i); 217 | h = 31 * h + b; 218 | } 219 | 220 | return h; 221 | 222 | } 223 | 224 | @State(Scope.Benchmark) 225 | public static class BenchmarkState { 226 | 227 | char[] array; 228 | byte[] barray; 229 | 230 | ByteBuffer bb; 231 | 232 | public BenchmarkState() { 233 | array = new char[64]; 234 | barray = new byte[64]; 235 | for (int k = 0; k < array.length; ++k) { 236 | array[k] = (char) k; 237 | barray[k] = (byte) k; 238 | } 239 | bb = ByteBuffer.wrap(barray); 240 | } 241 | 242 | } 243 | 244 | public static void main(String[] args) throws RunnerException { 245 | Options opt = new OptionsBuilder() 246 | .include(InterleavedHash.class.getSimpleName()) 247 | .warmupIterations(5).measurementIterations(5).forks(1).build(); 248 | 249 | new Runner(opt).run(); 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/algorithms/MersenneTwisterFast.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.algorithms; 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | /** 7 | *

MersenneTwister and MersenneTwisterFast

8 | *

9 | * Version 22, based on version MT199937(99/10/29) of the Mersenne 10 | * Twister algorithm found at The Mersenne Twister 12 | * Home Page, with the initialization improved using the new 2002/1/26 13 | * initialization algorithm By Sean Luke, October 2004. 14 | * 15 | *

16 | * MersenneTwister is a drop-in subclass replacement for 17 | * java.util.Random. It is properly synchronized and can be used in a 18 | * multithreaded environment. On modern VMs such as HotSpot, it is approximately 19 | * 1/3 slower than java.util.Random. 20 | * 21 | *

22 | * MersenneTwisterFast is not a subclass of java.util.Random. It has the 23 | * same public methods as Random does, however, and it is algorithmically 24 | * identical to MersenneTwister. MersenneTwisterFast has hard-code inlined all 25 | * of its methods directly, and made all of them final (well, the ones of 26 | * consequence anyway). Further, these methods are not synchronized, so 27 | * the same MersenneTwisterFast instance cannot be shared by multiple threads. 28 | * But all this helps MersenneTwisterFast achieve well over twice the speed of 29 | * MersenneTwister. java.util.Random is about 1/3 slower than 30 | * MersenneTwisterFast. 31 | * 32 | *

About the Mersenne Twister

33 | *

34 | * This is a Java version of the C-program for MT19937: Integer version. The 35 | * MT19937 algorithm was created by Makoto Matsumoto and Takuji Nishimura, who 36 | * ask: "When you use this, send an email to: matumoto@math.keio.ac.jp with an 37 | * appropriate reference to your work". Indicate that this is a translation of 38 | * their algorithm into Java. 39 | * 40 | *

41 | * Reference. Makato Matsumoto and Takuji Nishimura, "Mersenne Twister: 42 | * A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator", 43 | * ACM Transactions on Modeling and. Computer Simulation, Vol. 8, No. 1, 44 | * January 1998, pp 3--30. 45 | * 46 | *

About this Version

47 | * 48 | *

49 | * Changes since V21: Minor documentation HTML fixes. 50 | * 51 | *

52 | * Changes since V20: Added clearGuassian(). Modified stateEquals() to be 53 | * synchronizd on both objects for MersenneTwister, and changed its 54 | * documentation. Added synchronization to both setSeed() methods, to 55 | * writeState(), and to readState() in MersenneTwister. Removed synchronization 56 | * from readObject() in MersenneTwister. 57 | * 58 | *

59 | * Changes since V19: nextFloat(boolean, boolean) now returns float, not 60 | * double. 61 | * 62 | *

63 | * Changes since V18: Removed old final declarations, which used to 64 | * potentially speed up the code, but no longer. 65 | * 66 | *

67 | * Changes since V17: Removed vestigial references to &= 0xffffffff 68 | * which stemmed from the original C code. The C code could not guarantee that 69 | * ints were 32 bit, hence the masks. The vestigial references in the Java code 70 | * were likely optimized out anyway. 71 | * 72 | *

73 | * Changes since V16: Added nextDouble(includeZero, includeOne) and 74 | * nextFloat(includeZero, includeOne) to allow for half-open, fully-closed, and 75 | * fully-open intervals. 76 | * 77 | *

78 | * Changes Since V15: Added serialVersionUID to quiet compiler warnings 79 | * from Sun's overly verbose compilers as of JDK 1.5. 80 | * 81 | *

82 | * Changes Since V14: made strictfp, with StrictMath.log and 83 | * StrictMath.sqrt in nextGaussian instead of Math.log and Math.sqrt. This is 84 | * largely just to be safe, as it presently makes no difference in the speed, 85 | * correctness, or results of the algorithm. 86 | * 87 | *

88 | * Changes Since V13: clone() method CloneNotSupportedException removed. 89 | * 90 | *

91 | * Changes Since V12: clone() method added. 92 | * 93 | *

94 | * Changes Since V11: stateEquals(...) method added. MersenneTwisterFast 95 | * is equal to other MersenneTwisterFasts with identical state; likewise 96 | * MersenneTwister is equal to other MersenneTwister with identical state. This 97 | * isn't equals(...) because that requires a contract of immutability to compare 98 | * by value. 99 | * 100 | *

101 | * Changes Since V10: A documentation error suggested that setSeed(int[]) 102 | * required an int[] array 624 long. In fact, the array can be any non-zero 103 | * length. The new version also checks for this fact. 104 | * 105 | *

106 | * Changes Since V9: readState(stream) and writeState(stream) provided. 107 | * 108 | *

109 | * Changes Since V8: setSeed(int) was only using the first 28 bits of the 110 | * seed; it should have been 32 bits. For small-number seeds the behavior is 111 | * identical. 112 | * 113 | *

114 | * Changes Since V7: A documentation error in MersenneTwisterFast (but 115 | * not MersenneTwister) stated that nextDouble selects uniformly from the 116 | * full-open interval [0,1]. It does not. nextDouble's contract is identical 117 | * across MersenneTwisterFast, MersenneTwister, and java.util.Random, namely, 118 | * selection in the half-open interval [0,1). That is, 1.0 should not be 119 | * returned. A similar contract exists in nextFloat. 120 | * 121 | *

122 | * Changes Since V6: License has changed from LGPL to BSD. New timing 123 | * information to compare against java.util.Random. Recent versions of HotSpot 124 | * have helped Random increase in speed to the point where it is faster than 125 | * MersenneTwister but slower than MersenneTwisterFast (which should be the 126 | * case, as it's a less complex algorithm but is synchronized). 127 | * 128 | *

129 | * Changes Since V5: New empty constructor made to work the same as 130 | * java.util.Random -- namely, it seeds based on the current time in 131 | * milliseconds. 132 | * 133 | *

134 | * Changes Since V4: New initialization algorithms. See (see 136 | * http://www.math.keio.ac.jp/matumoto/MT2002/emt19937ar.html) 137 | * 138 | *

139 | * The MersenneTwister code is based on standard MT19937 C/C++ code by Takuji 140 | * Nishimura, with suggestions from Topher Cooper and Marc Rieffel, July 1997. 141 | * The code was originally translated into Java by Michael Lecuyer, January 142 | * 1999, and the original code is Copyright (c) 1999 by Michael Lecuyer. 143 | * 144 | *

Java notes

145 | * 146 | *

147 | * This implementation implements the bug fixes made in Java 1.2's version of 148 | * Random, which means it can be used with earlier versions of Java. See 151 | * the JDK 1.2 java.util.Random documentation for further documentation on 152 | * the random-number generation contracts made. Additionally, there's an 153 | * undocumented bug in the JDK java.util.Random.nextBytes() method, which this 154 | * code fixes. 155 | * 156 | *

157 | * Just like java.util.Random, this generator accepts a long seed but doesn't 158 | * use all of it. java.util.Random uses 48 bits. The Mersenne Twister instead 159 | * uses 32 bits (int size). So it's best if your seed does not exceed the int 160 | * range. 161 | * 162 | *

163 | * MersenneTwister can be used reliably on JDK version 1.1.5 or above. Earlier 164 | * Java versions have serious bugs in java.util.Random; only MersenneTwisterFast 165 | * (and not MersenneTwister nor java.util.Random) should be used with them. 166 | * 167 | *

License

168 | * 169 | * Copyright (c) 2003 by Sean Luke.
170 | * Portions copyright (c) 1993 by Michael Lecuyer.
171 | * All rights reserved.
172 | * 173 | *

174 | * Redistribution and use in source and binary forms, with or without 175 | * modification, are permitted provided that the following conditions are met: 176 | *

186 | *

187 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 188 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 189 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR CONTRIBUTORS BE 191 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 192 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 193 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 194 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 195 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 196 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 197 | * POSSIBILITY OF SUCH DAMAGE. 198 | * 199 | * @version 22 200 | */ 201 | 202 | // Note: this class is hard-inlined in all of its methods. This makes some of 203 | // the methods well-nigh unreadable in their complexity. In fact, the Mersenne 204 | // Twister is fairly easy code to understand: if you're trying to get a handle 205 | // on the code, I strongly suggest looking at MersenneTwister.java first. 206 | // -- Sean 207 | 208 | public final strictfp class MersenneTwisterFast implements Serializable, Cloneable { 209 | // Serialization 210 | private static final long serialVersionUID = -8219700664442619525L; // locked 211 | // as of 212 | // Version 213 | // 15 214 | 215 | // Period parameters 216 | private static final int N = 624; 217 | private static final int M = 397; 218 | private static final int MATRIX_A = 0x9908b0df; // private static final * 219 | // constant vector a 220 | private static final int UPPER_MASK = 0x80000000; // most significant w-r 221 | // bits 222 | private static final int LOWER_MASK = 0x7fffffff; // least significant r 223 | // bits 224 | 225 | // Tempering parameters 226 | private static final int TEMPERING_MASK_B = 0x9d2c5680; 227 | private static final int TEMPERING_MASK_C = 0xefc60000; 228 | 229 | private int mt[]; // the array for the state vector 230 | private int mti; // mti==N+1 means mt[N] is not initialized 231 | private int mag01[]; 232 | 233 | // a good initial seed (of int size, though stored in a long) 234 | // private static final long GOOD_SEED = 4357; 235 | 236 | private double __nextNextGaussian; 237 | private boolean __haveNextNextGaussian; 238 | 239 | /* 240 | * We're overriding all internal data, to my knowledge, so this should be 241 | * okay 242 | */ 243 | public Object clone() { 244 | try { 245 | MersenneTwisterFast f = (MersenneTwisterFast) (super.clone()); 246 | f.mt = (int[]) (mt.clone()); 247 | f.mag01 = (int[]) (mag01.clone()); 248 | return f; 249 | } catch (CloneNotSupportedException e) { 250 | throw new InternalError(); 251 | } // should never happen 252 | } 253 | 254 | /** 255 | * Returns true if the MersenneTwisterFast's current internal state is equal 256 | * to another MersenneTwisterFast. This is roughly the same as 257 | * equals(other), except that it compares based on value but does not 258 | * guarantee the contract of immutability (obviously random number 259 | * generators are immutable). Note that this does NOT check to see if the 260 | * internal gaussian storage is the same for both. You can guarantee that 261 | * the internal gaussian storage is the same (and so the nextGaussian() 262 | * methods will return the same values) by calling clearGaussian() on both 263 | * objects. 264 | */ 265 | public boolean stateEquals(MersenneTwisterFast other) { 266 | if (other == this) 267 | return true; 268 | if (other == null) 269 | return false; 270 | 271 | if (mti != other.mti) 272 | return false; 273 | for (int x = 0; x < mag01.length; x++) 274 | if (mag01[x] != other.mag01[x]) 275 | return false; 276 | for (int x = 0; x < mt.length; x++) 277 | if (mt[x] != other.mt[x]) 278 | return false; 279 | return true; 280 | } 281 | 282 | /** Reads the entire state of the MersenneTwister RNG from the stream */ 283 | public void readState(DataInputStream stream) throws IOException { 284 | int len = mt.length; 285 | for (int x = 0; x < len; x++) 286 | mt[x] = stream.readInt(); 287 | 288 | len = mag01.length; 289 | for (int x = 0; x < len; x++) 290 | mag01[x] = stream.readInt(); 291 | 292 | mti = stream.readInt(); 293 | __nextNextGaussian = stream.readDouble(); 294 | __haveNextNextGaussian = stream.readBoolean(); 295 | } 296 | 297 | /** Writes the entire state of the MersenneTwister RNG to the stream */ 298 | public void writeState(DataOutputStream stream) throws IOException { 299 | int len = mt.length; 300 | for (int x = 0; x < len; x++) 301 | stream.writeInt(mt[x]); 302 | 303 | len = mag01.length; 304 | for (int x = 0; x < len; x++) 305 | stream.writeInt(mag01[x]); 306 | 307 | stream.writeInt(mti); 308 | stream.writeDouble(__nextNextGaussian); 309 | stream.writeBoolean(__haveNextNextGaussian); 310 | } 311 | 312 | /** 313 | * Constructor using the default seed. 314 | */ 315 | public MersenneTwisterFast() { 316 | this(System.currentTimeMillis()); 317 | } 318 | 319 | /** 320 | * Constructor using a given seed. Though you pass this seed in as a long, 321 | * it's best to make sure it's actually an integer. 322 | * 323 | */ 324 | public MersenneTwisterFast(long seed) { 325 | setSeed(seed); 326 | } 327 | 328 | /** 329 | * Constructor using an array of integers as seed. Your array must have a 330 | * non-zero length. Only the first 624 integers in the array are used; if 331 | * the array is shorter than this then integers are repeatedly used in a 332 | * wrap-around fashion. 333 | */ 334 | public MersenneTwisterFast(int[] array) { 335 | setSeed(array); 336 | } 337 | 338 | /** 339 | * Initalize the pseudo random number generator. Don't pass in a long that's 340 | * bigger than an int (Mersenne Twister only uses the first 32 bits for its 341 | * seed). 342 | */ 343 | 344 | public void setSeed(long seed) { 345 | // Due to a bug in java.util.Random clear up to 1.2, we're 346 | // doing our own Gaussian variable. 347 | __haveNextNextGaussian = false; 348 | 349 | mt = new int[N]; 350 | 351 | mag01 = new int[2]; 352 | mag01[0] = 0x0; 353 | mag01[1] = MATRIX_A; 354 | 355 | mt[0] = (int) (seed & 0xffffffff); 356 | for (mti = 1; mti < N; mti++) { 357 | mt[mti] = (1812433253 * (mt[mti - 1] ^ (mt[mti - 1] >>> 30)) + mti); 358 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 359 | /* In the previous versions, MSBs of the seed affect */ 360 | /* only MSBs of the array mt[]. */ 361 | /* 2002/01/09 modified by Makoto Matsumoto */ 362 | // mt[mti] &= 0xffffffff; 363 | /* for >32 bit machines */ 364 | } 365 | } 366 | 367 | /** 368 | * Sets the seed of the MersenneTwister using an array of integers. Your 369 | * array must have a non-zero length. Only the first 624 integers in the 370 | * array are used; if the array is shorter than this then integers are 371 | * repeatedly used in a wrap-around fashion. 372 | */ 373 | 374 | public void setSeed(int[] array) { 375 | if (array.length == 0) 376 | throw new IllegalArgumentException( 377 | "Array length must be greater than zero"); 378 | int i, j, k; 379 | setSeed(19650218); 380 | i = 1; 381 | j = 0; 382 | k = (N > array.length ? N : array.length); 383 | for (; k != 0; k--) { 384 | mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1664525)) 385 | + array[j] + j; /* non linear */ 386 | // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ 387 | i++; 388 | j++; 389 | if (i >= N) { 390 | mt[0] = mt[N - 1]; 391 | i = 1; 392 | } 393 | if (j >= array.length) 394 | j = 0; 395 | } 396 | for (k = N - 1; k != 0; k--) { 397 | mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1566083941)) 398 | - i; /* non linear */ 399 | // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ 400 | i++; 401 | if (i >= N) { 402 | mt[0] = mt[N - 1]; 403 | i = 1; 404 | } 405 | } 406 | mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ 407 | } 408 | 409 | private int reloadInt() { 410 | int y; 411 | int kk; 412 | final int[] mt = this.mt; // locals are slightly faster 413 | final int[] mag01 = this.mag01; // locals are slightly faster 414 | 415 | for (kk = 0; kk < N - M; kk++) { 416 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 417 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 418 | } 419 | for (; kk < N - 1; kk++) { 420 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 421 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 422 | } 423 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 424 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 425 | 426 | mti = 0; 427 | y = mt[mti++]; 428 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 429 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 430 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 431 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 432 | 433 | return y; 434 | 435 | } 436 | 437 | public int nextInt() { 438 | if (mti >= N) // generate N words at one time 439 | { 440 | return reloadInt(); 441 | } 442 | int y = mt[mti++]; 443 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 444 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 445 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 446 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 447 | return y; 448 | } 449 | 450 | public short nextShort() { 451 | int y; 452 | 453 | if (mti >= N) // generate N words at one time 454 | { 455 | int kk; 456 | final int[] mt = this.mt; // locals are slightly faster 457 | final int[] mag01 = this.mag01; // locals are slightly faster 458 | 459 | for (kk = 0; kk < N - M; kk++) { 460 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 461 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 462 | } 463 | for (; kk < N - 1; kk++) { 464 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 465 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 466 | } 467 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 468 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 469 | 470 | mti = 0; 471 | } 472 | 473 | y = mt[mti++]; 474 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 475 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 476 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 477 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 478 | 479 | return (short) (y >>> 16); 480 | } 481 | 482 | public char nextChar() { 483 | int y; 484 | 485 | if (mti >= N) // generate N words at one time 486 | { 487 | int kk; 488 | final int[] mt = this.mt; // locals are slightly faster 489 | final int[] mag01 = this.mag01; // locals are slightly faster 490 | 491 | for (kk = 0; kk < N - M; kk++) { 492 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 493 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 494 | } 495 | for (; kk < N - 1; kk++) { 496 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 497 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 498 | } 499 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 500 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 501 | 502 | mti = 0; 503 | } 504 | 505 | y = mt[mti++]; 506 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 507 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 508 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 509 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 510 | 511 | return (char) (y >>> 16); 512 | } 513 | 514 | public boolean nextBoolean() { 515 | int y; 516 | 517 | if (mti >= N) // generate N words at one time 518 | { 519 | int kk; 520 | final int[] mt = this.mt; // locals are slightly faster 521 | final int[] mag01 = this.mag01; // locals are slightly faster 522 | 523 | for (kk = 0; kk < N - M; kk++) { 524 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 525 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 526 | } 527 | for (; kk < N - 1; kk++) { 528 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 529 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 530 | } 531 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 532 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 533 | 534 | mti = 0; 535 | } 536 | 537 | y = mt[mti++]; 538 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 539 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 540 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 541 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 542 | 543 | return (boolean) ((y >>> 31) != 0); 544 | } 545 | 546 | /** 547 | * This generates a coin flip with a probability probability of 548 | * returning true, else returning false. probability must be 549 | * between 0.0 and 1.0, inclusive. Not as precise a random real event as 550 | * nextBoolean(double), but twice as fast. To explicitly use this, remember 551 | * you may need to cast to float first. 552 | */ 553 | 554 | public boolean nextBoolean(float probability) { 555 | int y; 556 | 557 | if (probability < 0.0f || probability > 1.0f) 558 | throw new IllegalArgumentException( 559 | "probability must be between 0.0 and 1.0 inclusive."); 560 | if (probability == 0.0f) 561 | return false; // fix half-open issues 562 | else if (probability == 1.0f) 563 | return true; // fix half-open issues 564 | if (mti >= N) // generate N words at one time 565 | { 566 | int kk; 567 | final int[] mt = this.mt; // locals are slightly faster 568 | final int[] mag01 = this.mag01; // locals are slightly faster 569 | 570 | for (kk = 0; kk < N - M; kk++) { 571 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 572 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 573 | } 574 | for (; kk < N - 1; kk++) { 575 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 576 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 577 | } 578 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 579 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 580 | 581 | mti = 0; 582 | } 583 | 584 | y = mt[mti++]; 585 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 586 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 587 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 588 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 589 | 590 | return (y >>> 8) / ((float) (1 << 24)) < probability; 591 | } 592 | 593 | /** 594 | * This generates a coin flip with a probability probability of 595 | * returning true, else returning false. probability must be 596 | * between 0.0 and 1.0, inclusive. 597 | */ 598 | 599 | public boolean nextBoolean(double probability) { 600 | int y; 601 | int z; 602 | 603 | if (probability < 0.0 || probability > 1.0) 604 | throw new IllegalArgumentException( 605 | "probability must be between 0.0 and 1.0 inclusive."); 606 | if (probability == 0.0) 607 | return false; // fix half-open issues 608 | else if (probability == 1.0) 609 | return true; // fix half-open issues 610 | if (mti >= N) // generate N words at one time 611 | { 612 | int kk; 613 | final int[] mt = this.mt; // locals are slightly faster 614 | final int[] mag01 = this.mag01; // locals are slightly faster 615 | 616 | for (kk = 0; kk < N - M; kk++) { 617 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 618 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 619 | } 620 | for (; kk < N - 1; kk++) { 621 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 622 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 623 | } 624 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 625 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 626 | 627 | mti = 0; 628 | } 629 | 630 | y = mt[mti++]; 631 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 632 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 633 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 634 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 635 | 636 | if (mti >= N) // generate N words at one time 637 | { 638 | int kk; 639 | final int[] mt = this.mt; // locals are slightly faster 640 | final int[] mag01 = this.mag01; // locals are slightly faster 641 | 642 | for (kk = 0; kk < N - M; kk++) { 643 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 644 | mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; 645 | } 646 | for (; kk < N - 1; kk++) { 647 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 648 | mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 649 | } 650 | z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 651 | mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; 652 | 653 | mti = 0; 654 | } 655 | 656 | z = mt[mti++]; 657 | z ^= z >>> 11; // TEMPERING_SHIFT_U(z) 658 | z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) 659 | z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) 660 | z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) 661 | 662 | /* derived from nextDouble documentation in jdk 1.2 docs, see top */ 663 | return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53) < probability; 664 | } 665 | 666 | public byte nextByte() { 667 | int y; 668 | 669 | if (mti >= N) // generate N words at one time 670 | { 671 | int kk; 672 | final int[] mt = this.mt; // locals are slightly faster 673 | final int[] mag01 = this.mag01; // locals are slightly faster 674 | 675 | for (kk = 0; kk < N - M; kk++) { 676 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 677 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 678 | } 679 | for (; kk < N - 1; kk++) { 680 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 681 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 682 | } 683 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 684 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 685 | 686 | mti = 0; 687 | } 688 | 689 | y = mt[mti++]; 690 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 691 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 692 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 693 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 694 | 695 | return (byte) (y >>> 24); 696 | } 697 | 698 | public void nextBytes(byte[] bytes) { 699 | int y; 700 | 701 | for (int x = 0; x < bytes.length; x++) { 702 | if (mti >= N) // generate N words at one time 703 | { 704 | int kk; 705 | final int[] mt = this.mt; // locals are slightly faster 706 | final int[] mag01 = this.mag01; // locals are slightly faster 707 | 708 | for (kk = 0; kk < N - M; kk++) { 709 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 710 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 711 | } 712 | for (; kk < N - 1; kk++) { 713 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 714 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 715 | } 716 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 717 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 718 | 719 | mti = 0; 720 | } 721 | 722 | y = mt[mti++]; 723 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 724 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 725 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 726 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 727 | 728 | bytes[x] = (byte) (y >>> 24); 729 | } 730 | } 731 | 732 | /** 733 | * Returns a long drawn uniformly from 0 to n-1. Suffice it to say, n must 734 | * be greater than 0, or an IllegalArgumentException is raised. 735 | */ 736 | 737 | public long nextLong() { 738 | int y; 739 | int z; 740 | 741 | if (mti >= N) // generate N words at one time 742 | { 743 | int kk; 744 | final int[] mt = this.mt; // locals are slightly faster 745 | final int[] mag01 = this.mag01; // locals are slightly faster 746 | 747 | for (kk = 0; kk < N - M; kk++) { 748 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 749 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 750 | } 751 | for (; kk < N - 1; kk++) { 752 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 753 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 754 | } 755 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 756 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 757 | 758 | mti = 0; 759 | } 760 | 761 | y = mt[mti++]; 762 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 763 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 764 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 765 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 766 | 767 | if (mti >= N) // generate N words at one time 768 | { 769 | int kk; 770 | final int[] mt = this.mt; // locals are slightly faster 771 | final int[] mag01 = this.mag01; // locals are slightly faster 772 | 773 | for (kk = 0; kk < N - M; kk++) { 774 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 775 | mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; 776 | } 777 | for (; kk < N - 1; kk++) { 778 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 779 | mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 780 | } 781 | z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 782 | mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; 783 | 784 | mti = 0; 785 | } 786 | 787 | z = mt[mti++]; 788 | z ^= z >>> 11; // TEMPERING_SHIFT_U(z) 789 | z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) 790 | z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) 791 | z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) 792 | 793 | return (((long) y) << 32) + (long) z; 794 | } 795 | 796 | /** 797 | * Returns a long drawn uniformly from 0 to n-1. Suffice it to say, n must 798 | * be > 0, or an IllegalArgumentException is raised. 799 | */ 800 | public long nextLong(long n) { 801 | if (n <= 0) 802 | throw new IllegalArgumentException("n must be positive, got: " + n); 803 | 804 | long bits, val; 805 | do { 806 | int y; 807 | int z; 808 | 809 | if (mti >= N) // generate N words at one time 810 | { 811 | int kk; 812 | final int[] mt = this.mt; // locals are slightly faster 813 | final int[] mag01 = this.mag01; // locals are slightly faster 814 | 815 | for (kk = 0; kk < N - M; kk++) { 816 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 817 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 818 | } 819 | for (; kk < N - 1; kk++) { 820 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 821 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 822 | } 823 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 824 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 825 | 826 | mti = 0; 827 | } 828 | 829 | y = mt[mti++]; 830 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 831 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 832 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 833 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 834 | 835 | if (mti >= N) // generate N words at one time 836 | { 837 | int kk; 838 | final int[] mt = this.mt; // locals are slightly faster 839 | final int[] mag01 = this.mag01; // locals are slightly faster 840 | 841 | for (kk = 0; kk < N - M; kk++) { 842 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 843 | mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; 844 | } 845 | for (; kk < N - 1; kk++) { 846 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 847 | mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 848 | } 849 | z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 850 | mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; 851 | 852 | mti = 0; 853 | } 854 | 855 | z = mt[mti++]; 856 | z ^= z >>> 11; // TEMPERING_SHIFT_U(z) 857 | z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) 858 | z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) 859 | z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) 860 | 861 | bits = (((((long) y) << 32) + (long) z) >>> 1); 862 | val = bits % n; 863 | } while (bits - val + (n - 1) < 0); 864 | return val; 865 | } 866 | 867 | /** 868 | * Returns a random double in the half-open range from [0.0,1.0). Thus 0.0 869 | * is a valid result but 1.0 is not. 870 | */ 871 | public double nextDouble() { 872 | int y; 873 | int z; 874 | 875 | if (mti >= N) // generate N words at one time 876 | { 877 | int kk; 878 | final int[] mt = this.mt; // locals are slightly faster 879 | final int[] mag01 = this.mag01; // locals are slightly faster 880 | 881 | for (kk = 0; kk < N - M; kk++) { 882 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 883 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 884 | } 885 | for (; kk < N - 1; kk++) { 886 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 887 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 888 | } 889 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 890 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 891 | 892 | mti = 0; 893 | } 894 | 895 | y = mt[mti++]; 896 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 897 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 898 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 899 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 900 | 901 | if (mti >= N) // generate N words at one time 902 | { 903 | int kk; 904 | final int[] mt = this.mt; // locals are slightly faster 905 | final int[] mag01 = this.mag01; // locals are slightly faster 906 | 907 | for (kk = 0; kk < N - M; kk++) { 908 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 909 | mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; 910 | } 911 | for (; kk < N - 1; kk++) { 912 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 913 | mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 914 | } 915 | z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 916 | mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; 917 | 918 | mti = 0; 919 | } 920 | 921 | z = mt[mti++]; 922 | z ^= z >>> 11; // TEMPERING_SHIFT_U(z) 923 | z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) 924 | z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) 925 | z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) 926 | 927 | /* derived from nextDouble documentation in jdk 1.2 docs, see top */ 928 | return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53); 929 | } 930 | 931 | /** 932 | * Returns a double in the range from 0.0 to 1.0, possibly inclusive of 0.0 933 | * and 1.0 themselves. Thus: 934 | * 935 | * 936 | * 937 | * 938 | * 939 | * 940 | * 941 | * 942 | * 943 | * 944 | * 945 | * 946 | * 947 | * 948 | * 949 | * 950 | * 951 | * 952 | * 953 | * 954 | * 955 | * 956 | * 957 | *
ExpressionInterval
nextDouble(false, false)(0.0, 1.0)
nextDouble(true, false)[0.0, 1.0)
nextDouble(false, true)(0.0, 1.0]
nextDouble(true, true)[0.0, 1.0]
Table of intervals
958 | * 959 | *

960 | * This version preserves all possible random values in the double range. 961 | */ 962 | public double nextDouble(boolean includeZero, boolean includeOne) { 963 | double d = 0.0; 964 | do { 965 | d = nextDouble(); // grab a value, initially from half-open [0.0, 966 | // 1.0) 967 | if (includeOne && nextBoolean()) 968 | d += 1.0; // if includeOne, with 1/2 probability, push to [1.0, 969 | // 2.0) 970 | } while ((d > 1.0) || // everything above 1.0 is always invalid 971 | (!includeZero && d == 0.0)); // if we're not including zero, 0.0 972 | // is invalid 973 | return d; 974 | } 975 | 976 | /** 977 | * Clears the internal gaussian variable from the RNG. You only need to do 978 | * this in the rare case that you need to guarantee that two RNGs have 979 | * identical internal state. Otherwise, disregard this method. See 980 | * stateEquals(other). 981 | */ 982 | public void clearGaussian() { 983 | __haveNextNextGaussian = false; 984 | } 985 | 986 | public double nextGaussian() { 987 | if (__haveNextNextGaussian) { 988 | __haveNextNextGaussian = false; 989 | return __nextNextGaussian; 990 | } else { 991 | double v1, v2, s; 992 | do { 993 | int y; 994 | int z; 995 | int a; 996 | int b; 997 | 998 | if (mti >= N) // generate N words at one time 999 | { 1000 | int kk; 1001 | final int[] mt = this.mt; // locals are slightly faster 1002 | final int[] mag01 = this.mag01; // locals are slightly 1003 | // faster 1004 | 1005 | for (kk = 0; kk < N - M; kk++) { 1006 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1007 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 1008 | } 1009 | for (; kk < N - 1; kk++) { 1010 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1011 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 1012 | } 1013 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1014 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 1015 | 1016 | mti = 0; 1017 | } 1018 | 1019 | y = mt[mti++]; 1020 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 1021 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 1022 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 1023 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 1024 | 1025 | if (mti >= N) // generate N words at one time 1026 | { 1027 | int kk; 1028 | final int[] mt = this.mt; // locals are slightly faster 1029 | final int[] mag01 = this.mag01; // locals are slightly 1030 | // faster 1031 | 1032 | for (kk = 0; kk < N - M; kk++) { 1033 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1034 | mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1]; 1035 | } 1036 | for (; kk < N - 1; kk++) { 1037 | z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1038 | mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 1039 | } 1040 | z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1041 | mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1]; 1042 | 1043 | mti = 0; 1044 | } 1045 | 1046 | z = mt[mti++]; 1047 | z ^= z >>> 11; // TEMPERING_SHIFT_U(z) 1048 | z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z) 1049 | z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z) 1050 | z ^= (z >>> 18); // TEMPERING_SHIFT_L(z) 1051 | 1052 | if (mti >= N) // generate N words at one time 1053 | { 1054 | int kk; 1055 | final int[] mt = this.mt; // locals are slightly faster 1056 | final int[] mag01 = this.mag01; // locals are slightly 1057 | // faster 1058 | 1059 | for (kk = 0; kk < N - M; kk++) { 1060 | a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1061 | mt[kk] = mt[kk + M] ^ (a >>> 1) ^ mag01[a & 0x1]; 1062 | } 1063 | for (; kk < N - 1; kk++) { 1064 | a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1065 | mt[kk] = mt[kk + (M - N)] ^ (a >>> 1) ^ mag01[a & 0x1]; 1066 | } 1067 | a = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1068 | mt[N - 1] = mt[M - 1] ^ (a >>> 1) ^ mag01[a & 0x1]; 1069 | 1070 | mti = 0; 1071 | } 1072 | 1073 | a = mt[mti++]; 1074 | a ^= a >>> 11; // TEMPERING_SHIFT_U(a) 1075 | a ^= (a << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(a) 1076 | a ^= (a << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(a) 1077 | a ^= (a >>> 18); // TEMPERING_SHIFT_L(a) 1078 | 1079 | if (mti >= N) // generate N words at one time 1080 | { 1081 | int kk; 1082 | final int[] mt = this.mt; // locals are slightly faster 1083 | final int[] mag01 = this.mag01; // locals are slightly 1084 | // faster 1085 | 1086 | for (kk = 0; kk < N - M; kk++) { 1087 | b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1088 | mt[kk] = mt[kk + M] ^ (b >>> 1) ^ mag01[b & 0x1]; 1089 | } 1090 | for (; kk < N - 1; kk++) { 1091 | b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1092 | mt[kk] = mt[kk + (M - N)] ^ (b >>> 1) ^ mag01[b & 0x1]; 1093 | } 1094 | b = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1095 | mt[N - 1] = mt[M - 1] ^ (b >>> 1) ^ mag01[b & 0x1]; 1096 | 1097 | mti = 0; 1098 | } 1099 | 1100 | b = mt[mti++]; 1101 | b ^= b >>> 11; // TEMPERING_SHIFT_U(b) 1102 | b ^= (b << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(b) 1103 | b ^= (b << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(b) 1104 | b ^= (b >>> 18); // TEMPERING_SHIFT_L(b) 1105 | 1106 | /* 1107 | * derived from nextDouble documentation in jdk 1.2 docs, see 1108 | * top 1109 | */ 1110 | v1 = 2 * (((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53)) - 1; 1111 | v2 = 2 * (((((long) (a >>> 6)) << 27) + (b >>> 5)) / (double) (1L << 53)) - 1; 1112 | s = v1 * v1 + v2 * v2; 1113 | } while (s >= 1 || s == 0); 1114 | double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s) / s); 1115 | __nextNextGaussian = v2 * multiplier; 1116 | __haveNextNextGaussian = true; 1117 | return v1 * multiplier; 1118 | } 1119 | } 1120 | 1121 | /** 1122 | * Returns a random float in the half-open range from [0.0f,1.0f). Thus 0.0f 1123 | * is a valid result but 1.0f is not. 1124 | */ 1125 | public float nextFloat() { 1126 | int y; 1127 | 1128 | if (mti >= N) // generate N words at one time 1129 | { 1130 | int kk; 1131 | final int[] mt = this.mt; // locals are slightly faster 1132 | final int[] mag01 = this.mag01; // locals are slightly faster 1133 | 1134 | for (kk = 0; kk < N - M; kk++) { 1135 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1136 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 1137 | } 1138 | for (; kk < N - 1; kk++) { 1139 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1140 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 1141 | } 1142 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1143 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 1144 | 1145 | mti = 0; 1146 | } 1147 | 1148 | y = mt[mti++]; 1149 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 1150 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 1151 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 1152 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 1153 | 1154 | return (y >>> 8) / ((float) (1 << 24)); 1155 | } 1156 | 1157 | /** 1158 | * Returns a float in the range from 0.0f to 1.0f, possibly inclusive of 1159 | * 0.0f and 1.0f themselves. Thus: 1160 | * 1161 | * 1162 | * 1163 | * 1164 | * 1165 | * 1166 | * 1167 | * 1168 | * 1169 | * 1170 | * 1171 | * 1172 | * 1173 | * 1174 | * 1175 | * 1176 | * 1177 | * 1178 | * 1179 | * 1180 | * 1181 | * 1182 | * 1183 | *
ExpressionInterval
nextFloat(false, false)(0.0f, 1.0f)
nextFloat(true, false)[0.0f, 1.0f)
nextFloat(false, true)(0.0f, 1.0f]
nextFloat(true, true)[0.0f, 1.0f]
Table of intervals
1184 | * 1185 | *

1186 | * This version preserves all possible random values in the float range. 1187 | */ 1188 | public float nextFloat(boolean includeZero, boolean includeOne) { 1189 | float d = 0.0f; 1190 | do { 1191 | d = nextFloat(); // grab a value, initially from half-open [0.0f, 1192 | // 1.0f) 1193 | if (includeOne && nextBoolean()) 1194 | d += 1.0f; // if includeOne, with 1/2 probability, push to 1195 | // [1.0f, 2.0f) 1196 | } while ((d > 1.0f) || // everything above 1.0f is always invalid 1197 | (!includeZero && d == 0.0f)); // if we're not including zero, 1198 | // 0.0f is invalid 1199 | return d; 1200 | } 1201 | 1202 | /** 1203 | * Returns an integer drawn uniformly from 0 to n-1. Suffice it to say, n 1204 | * must be > 0, or an IllegalArgumentException is raised. 1205 | */ 1206 | public int nextInt(int n) { 1207 | if (n <= 0) 1208 | throw new IllegalArgumentException("n must be positive, got: " + n); 1209 | 1210 | if ((n & -n) == n) // i.e., n is a power of 2 1211 | { 1212 | int y; 1213 | 1214 | if (mti >= N) // generate N words at one time 1215 | { 1216 | int kk; 1217 | final int[] mt = this.mt; // locals are slightly faster 1218 | final int[] mag01 = this.mag01; // locals are slightly faster 1219 | 1220 | for (kk = 0; kk < N - M; kk++) { 1221 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1222 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 1223 | } 1224 | for (; kk < N - 1; kk++) { 1225 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1226 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 1227 | } 1228 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1229 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 1230 | 1231 | mti = 0; 1232 | } 1233 | 1234 | y = mt[mti++]; 1235 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 1236 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 1237 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 1238 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 1239 | 1240 | return (int) ((n * (long) (y >>> 1)) >> 31); 1241 | } 1242 | 1243 | int bits, val; 1244 | do { 1245 | int y; 1246 | 1247 | if (mti >= N) // generate N words at one time 1248 | { 1249 | int kk; 1250 | final int[] mt = this.mt; // locals are slightly faster 1251 | final int[] mag01 = this.mag01; // locals are slightly faster 1252 | 1253 | for (kk = 0; kk < N - M; kk++) { 1254 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1255 | mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1]; 1256 | } 1257 | for (; kk < N - 1; kk++) { 1258 | y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 1259 | mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 1260 | } 1261 | y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 1262 | mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; 1263 | 1264 | mti = 0; 1265 | } 1266 | 1267 | y = mt[mti++]; 1268 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) 1269 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) 1270 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) 1271 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) 1272 | 1273 | bits = (y >>> 1); 1274 | val = bits % n; 1275 | } while (bits - val + (n - 1) < 0); 1276 | return val; 1277 | } 1278 | 1279 | } 1280 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/algorithms/Shuffle.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.algorithms; 2 | 3 | import java.util.Random; 4 | import java.util.concurrent.ThreadLocalRandom; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.openjdk.jmh.annotations.Benchmark; 8 | import org.openjdk.jmh.annotations.BenchmarkMode; 9 | import org.openjdk.jmh.annotations.Mode; 10 | import org.openjdk.jmh.annotations.OutputTimeUnit; 11 | import org.openjdk.jmh.annotations.Scope; 12 | import org.openjdk.jmh.annotations.State; 13 | import org.openjdk.jmh.runner.Runner; 14 | import org.openjdk.jmh.runner.RunnerException; 15 | import org.openjdk.jmh.runner.options.Options; 16 | import org.openjdk.jmh.runner.options.OptionsBuilder; 17 | 18 | @State(Scope.Thread) 19 | @BenchmarkMode(Mode.AverageTime) 20 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 21 | public class Shuffle { 22 | 23 | private static void swap(int[] arr, int i, int j) { 24 | int tmp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = tmp; 27 | } 28 | 29 | public static void shuffle(int arr[], ThreadLocalRandom rnd) { 30 | int size = arr.length; 31 | // Shuffle array 32 | for (int i = size; i > 1; i--) 33 | swap(arr, i - 1, rnd.nextInt(i)); 34 | } 35 | 36 | public static void shuffle(int arr[], MersenneTwisterFast rnd) { 37 | int size = arr.length; 38 | 39 | // Shuffle array 40 | for (int i = size; i > 1; i--) 41 | swap(arr, i - 1, rnd.nextInt(i)); 42 | } 43 | 44 | static int fancy_ranged_random_mult_lazy(int range, MersenneTwisterFast rnd) { 45 | long random32bit, multiresult; 46 | long leftover; 47 | if((range & (range - 1)) == 0) 48 | return rnd.nextInt() & (range - 1); 49 | final long mask = 0xFFFFFFFFL; 50 | random32bit = rnd.nextInt() & mask; 51 | multiresult = random32bit * range; 52 | leftover = multiresult & mask; 53 | if(leftover < range) { 54 | final long threshold = 0xFFFFFFFF % range; 55 | while (leftover <= threshold) { 56 | random32bit = rnd.nextInt() & mask; 57 | multiresult = random32bit * range; 58 | leftover = multiresult & mask; 59 | } 60 | } 61 | return (int) (multiresult >>> 32); // [0, range) 62 | } 63 | 64 | static int ranged_random_mult_lazy(int range, MersenneTwisterFast rnd) { 65 | long random32bit, multiresult; 66 | long leftover; 67 | final long mask = 0xFFFFFFFFL; 68 | random32bit = rnd.nextInt() & mask; 69 | multiresult = random32bit * range; 70 | leftover = multiresult & mask; 71 | if(leftover < range) { 72 | final long threshold = (-range) % range; 73 | while (leftover <= threshold) { 74 | random32bit = rnd.nextInt() & mask; 75 | multiresult = random32bit * range; 76 | leftover = multiresult & mask; 77 | } 78 | } 79 | return (int) (multiresult >>> 32); // [0, range) 80 | } 81 | 82 | public static void shuffle_fastF(int arr[], MersenneTwisterFast rnd) { 83 | int size = arr.length; 84 | 85 | // Shuffle array 86 | for (int i = size; i > 1; i--) 87 | swap(arr, i - 1, ranged_random_mult_lazy(i,rnd)); 88 | } 89 | static int fancy_ranged_random_mult_lazy(int range, ThreadLocalRandom rnd) { 90 | long random32bit, multiresult; 91 | long leftover; 92 | if((range & (range - 1)) == 0) 93 | return rnd.nextInt() & (range - 1); 94 | final long mask = 0xFFFFFFFFL; 95 | random32bit = rnd.nextInt() & mask; 96 | multiresult = random32bit * range; 97 | leftover = multiresult & mask; 98 | if(leftover < range) { 99 | final long threshold = 0xFFFFFFFF % range; 100 | while (leftover <= threshold) { 101 | random32bit = rnd.nextInt() & mask; 102 | multiresult = random32bit * range; 103 | leftover = multiresult & mask; 104 | } 105 | } 106 | return (int) (multiresult >>> 32); // [0, range) 107 | } 108 | 109 | static int ranged_random_mult_lazy(int range, ThreadLocalRandom rnd) { 110 | long random32bit, multiresult; 111 | long leftover; 112 | final long mask = 0xFFFFFFFFL; 113 | random32bit = rnd.nextInt() & mask; 114 | multiresult = random32bit * range; 115 | leftover = multiresult & mask; 116 | if(leftover < range) { 117 | final long threshold = (-range) % range; 118 | while (leftover <= threshold) { 119 | random32bit = rnd.nextInt() & mask; 120 | multiresult = random32bit * range; 121 | leftover = multiresult & mask; 122 | } 123 | } 124 | return (int) (multiresult >>> 32); // [0, range) 125 | } 126 | 127 | public static void shuffle_fastF(int arr[], ThreadLocalRandom rnd) { 128 | int size = arr.length; 129 | 130 | // Shuffle array 131 | for (int i = size; i > 1; i--) 132 | swap(arr, i - 1, ranged_random_mult_lazy(i,rnd)); 133 | } 134 | 135 | 136 | @State(Scope.Benchmark) 137 | public static class BenchmarkState { 138 | int N = 1777216; 139 | int[] array = new int[N]; 140 | 141 | public BenchmarkState() { 142 | 143 | for (int k = 0; k < N; ++k) 144 | array[k] = k; 145 | } 146 | 147 | } 148 | static MersenneTwisterFast rr = new MersenneTwisterFast(); 149 | static ThreadLocalRandom r = ThreadLocalRandom.current(); 150 | 151 | 152 | @Benchmark 153 | public void basicshuffle(BenchmarkState s) { 154 | shuffle(s.array, r); 155 | } 156 | @Benchmark 157 | public void basicshuffle_MT(BenchmarkState s) { 158 | shuffle(s.array, rr); 159 | } 160 | 161 | 162 | @Benchmark 163 | public void fastFshuffle(BenchmarkState s) { 164 | shuffle_fastF(s.array, r); 165 | } 166 | 167 | @Benchmark 168 | public void fastFshuffle_MT(BenchmarkState s) { 169 | shuffle_fastF(s.array, rr); 170 | } 171 | 172 | 173 | public static void main(String[] args) throws RunnerException { 174 | Options opt = new OptionsBuilder() 175 | .include(Shuffle.class.getSimpleName()).warmupIterations(2) 176 | .measurementIterations(3).forks(1).build(); 177 | 178 | new Runner(opt).run(); 179 | } 180 | 181 | } 182 | 183 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/arrays/ArraysBench.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.arrays; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | import java.util.*; 5 | 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.BenchmarkMode; 8 | import org.openjdk.jmh.annotations.Mode; 9 | import org.openjdk.jmh.annotations.OutputTimeUnit; 10 | import org.openjdk.jmh.annotations.Scope; 11 | import org.openjdk.jmh.annotations.State; 12 | import org.openjdk.jmh.runner.Runner; 13 | import org.openjdk.jmh.runner.RunnerException; 14 | import org.openjdk.jmh.runner.options.Options; 15 | import org.openjdk.jmh.runner.options.OptionsBuilder; 16 | 17 | @State(Scope.Thread) 18 | @BenchmarkMode(Mode.AverageTime) 19 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 20 | public class ArraysBench { 21 | 22 | private static int sum(int[] arr) { 23 | int s = 0; 24 | for(int k = 0; k < arr.length; ++k) { 25 | s += arr[k]; 26 | } 27 | return s; 28 | } 29 | 30 | private static int sum2(int[] arr) { 31 | int s = 0; 32 | final int l = arr.length; 33 | for(int k = 0; k < l; ++k) { 34 | s += arr[k]; 35 | } 36 | return s; 37 | } 38 | 39 | private static int listsum(ArrayList arr) { 40 | int s = 0; 41 | for(int k = 0; k < arr.size(); ++k) { 42 | s += arr.get(k); 43 | } 44 | return s; 45 | } 46 | 47 | private static int listsum2(ArrayList arr) { 48 | int s = 0; 49 | final int l = arr.size(); 50 | for(int k = 0; k < l; ++k) { 51 | s += arr.get(k); 52 | } 53 | return s; 54 | } 55 | 56 | @State(Scope.Benchmark) 57 | public static class BenchmarkState { 58 | int N = 17773; 59 | int[] array = new int[N]; 60 | ArrayList list = new ArrayList(); 61 | 62 | public BenchmarkState() { 63 | 64 | for (int k = 0; k < N; ++k) { 65 | array[k] = k; 66 | list.add(k); 67 | } 68 | } 69 | } 70 | 71 | @Benchmark 72 | public int basicsum(BenchmarkState s) { 73 | return sum(s.array); 74 | } 75 | @Benchmark 76 | public int bufbasicsum(BenchmarkState s) { 77 | return sum2(s.array); 78 | } 79 | 80 | @Benchmark 81 | public int basiclistsum(BenchmarkState s) { 82 | return listsum(s.list); 83 | } 84 | @Benchmark 85 | public int bufbasiclistsum(BenchmarkState s) { 86 | return listsum2(s.list); 87 | } 88 | 89 | public static void main(String[] args) throws RunnerException { 90 | Options opt = new OptionsBuilder() 91 | .include(ArraysBench.class.getSimpleName()).warmupIterations(5) 92 | .measurementIterations(20).forks(1).build(); 93 | 94 | new Runner(opt).run(); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/bitset/Bitset.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.bitset; 2 | 3 | import java.util.BitSet; 4 | import org.openjdk.jmh.annotations.*; 5 | import org.openjdk.jmh.runner.Runner; 6 | import java.util.concurrent.TimeUnit; 7 | import org.openjdk.jmh.runner.RunnerException; 8 | import org.openjdk.jmh.runner.options.Options; 9 | import org.openjdk.jmh.runner.options.OptionsBuilder; 10 | 11 | 12 | 13 | @State(Scope.Thread) 14 | @BenchmarkMode(Mode.AverageTime) 15 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 16 | public class Bitset { 17 | static BitSet b = new BitSet(); 18 | 19 | @Setup 20 | public void populate() { 21 | for (int i = 0; i <= 100000000; i+= 100){ 22 | b.set(i); 23 | } 24 | } 25 | 26 | 27 | @Benchmark 28 | public BitSet construct() { 29 | BitSet b = new BitSet(); 30 | for (int i = 0; i <= 100000000; i+= 100){ 31 | b.set(i); 32 | } 33 | return b; 34 | } 35 | 36 | @Benchmark 37 | public int count() { 38 | return b.cardinality(); 39 | } 40 | 41 | @Benchmark 42 | public int iterate() { 43 | int sum = 0; 44 | for (int i = b.nextSetBit(0); i >= 0; i = b.nextSetBit(i+1)) { 45 | sum++; 46 | } 47 | return sum; 48 | } 49 | 50 | public static void main(String[] args) throws RunnerException { 51 | Options opt = new OptionsBuilder() 52 | .include(Bitset.class.getSimpleName()) 53 | .warmupIterations(5).measurementIterations(5).forks(1).build(); 54 | 55 | new Runner(opt).run(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/bytebuffer/DirectVSHeapVSArray.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.bytebuffer; 2 | 3 | import java.nio.*; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.Scope; 7 | import org.openjdk.jmh.annotations.State; 8 | import org.openjdk.jmh.runner.Runner; 9 | import org.openjdk.jmh.runner.RunnerException; 10 | import org.openjdk.jmh.runner.options.Options; 11 | import org.openjdk.jmh.runner.options.OptionsBuilder; 12 | 13 | import me.lemire.hashing.InterleavedHash; 14 | 15 | 16 | public class DirectVSHeapVSArray { 17 | @State(Scope.Benchmark) 18 | public static class BenchmarkState { 19 | long[] array = new long[(1<<16)/64]; 20 | LongBuffer direct = ByteBuffer.allocateDirect((1<<16)/8).asLongBuffer(); 21 | LongBuffer heap = ByteBuffer.allocate((1<<16)/8).asLongBuffer(); 22 | } 23 | 24 | @Benchmark 25 | public long arrayTest(BenchmarkState s) { 26 | long sum = 0; 27 | for(int k = 0; k < (1<<16)/64; ++k) { 28 | sum += s.array[k]; 29 | } 30 | return sum; 31 | } 32 | 33 | 34 | @Benchmark 35 | public long directTest(BenchmarkState s) { 36 | long sum = 0; 37 | for(int k = 0; k < (1<<16)/64; ++k) { 38 | sum += s.direct.get(k); 39 | } 40 | return sum; 41 | } 42 | 43 | @Benchmark 44 | public long heapTest(BenchmarkState s) { 45 | long sum = 0; 46 | for(int k = 0; k < (1<<16)/64; ++k) { 47 | sum += s.heap.get(k);; 48 | } 49 | return sum; 50 | } 51 | 52 | 53 | public static void main(String[] args) throws RunnerException { 54 | Options opt = new OptionsBuilder() 55 | .include(DirectVSHeapVSArray.class.getSimpleName()) 56 | .warmupIterations(5).measurementIterations(5).forks(1).build(); 57 | 58 | new Runner(opt).run(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/Array.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public interface Array { 4 | public int get(int i); 5 | public void set(int i, int x); 6 | public int size(); 7 | } -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/BasicSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class BasicSummer { 4 | protected Array array; 5 | 6 | public BasicSummer(Array backend) { 7 | array = backend; 8 | } 9 | 10 | 11 | public int compute() { 12 | for(int k = 0; k < array.size(); ++k) 13 | array.set(k,k); 14 | int sum = 0; 15 | for(int k = 0; k < array.size(); ++k) 16 | sum += array.get(k); 17 | return sum; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/FastSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class FastSummer { 4 | 5 | int[] array; 6 | public FastSummer(int cap) { 7 | array = new int[cap]; 8 | } 9 | 10 | public int compute() { 11 | for(int k = 0; k < array.length; ++k) 12 | array[k] = k; 13 | int sum = 0; 14 | for(int k = 0; k < array.length; ++k) 15 | sum += array[k]; 16 | return sum; 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/FixedSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public final class FixedSummer { 4 | protected final NaiveArray array; 5 | 6 | public FixedSummer(int cap) { 7 | array = new NaiveArray(cap); 8 | } 9 | 10 | 11 | public int compute() { 12 | for(int k = 0; k < array.size(); ++k) 13 | array.set(k,k); 14 | int sum = 0; 15 | for(int k = 0; k < array.size(); ++k) 16 | sum += array.get(k); 17 | return sum; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/NaiveArray.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public final class NaiveArray implements Array { 4 | protected int[] array; 5 | 6 | public NaiveArray(int cap) { 7 | array = new int[cap]; 8 | } 9 | 10 | public int get(int i) { 11 | return array[i]; 12 | } 13 | 14 | public void set(int i, int x) { 15 | array[i] = x; 16 | } 17 | 18 | public int size() { 19 | return array.length; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/ReverseBasicSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class ReverseBasicSummer { 4 | protected Array array; 5 | 6 | public ReverseBasicSummer(Array backend) { 7 | array = backend; 8 | } 9 | 10 | 11 | public int compute() { 12 | for(int k = array.size() - 1; k >= 0; --k) 13 | array.set(k,k); 14 | int sum = 0; 15 | for(int k = array.size() - 1; k >= 0; --k) 16 | sum += array.get(k); 17 | return sum; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/ReverseFastSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class ReverseFastSummer { 4 | 5 | int[] array; 6 | public ReverseFastSummer(int cap) { 7 | array = new int[cap]; 8 | } 9 | 10 | public int compute() { 11 | for(int k = array.length - 1 ; k >=0; --k) 12 | array[k] = k; 13 | int sum = 0; 14 | for(int k = array.length - 1 ; k >=0; --k) 15 | sum += array[k]; 16 | return sum; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/ReverseFixedSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public final class ReverseFixedSummer { 4 | protected final NaiveArray array; 5 | 6 | public ReverseFixedSummer(int cap) { 7 | array = new NaiveArray(cap); 8 | } 9 | 10 | 11 | public int compute() { 12 | for(int k = array.size() - 1; k >=0 ; --k) 13 | array.set(k,k); 14 | int sum = 0; 15 | for(int k = array.size() - 1; k >=0 ; --k) 16 | sum += array.get(k); 17 | return sum; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/ReverseSmartSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class ReverseSmartSummer { 4 | protected Array array; 5 | 6 | public ReverseSmartSummer(Array backend) { 7 | array = backend; 8 | } 9 | 10 | 11 | public int compute() { 12 | if(array instanceof NaiveArray) { 13 | int[] back = ((NaiveArray) array).array; 14 | for(int k = back.length - 1; k >= 0; --k) 15 | back[k] = k; 16 | int sum = 0; 17 | for(int k = back.length - 1; k >= 0; --k) 18 | sum += back[k]; 19 | return sum; 20 | } 21 | for(int k = array.size() - 1; k >= 0; --k) 22 | array.set(k,k); 23 | int sum = 0; 24 | for(int k = 0; k < array.size(); --k) 25 | sum += array.get(k); 26 | return sum; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/SillySummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class SillySummer { 4 | protected Array array; 5 | 6 | public SillySummer(Array backend) { 7 | array = backend; 8 | } 9 | 10 | 11 | public int compute() { 12 | if(array instanceof NaiveArray) { 13 | for(int k = 0; k < array.size(); ++k) 14 | array.set(k,k); 15 | int sum = 0; 16 | for(int k = 0; k < array.size(); ++k) 17 | sum += array.get(k); 18 | return sum; 19 | } 20 | for(int k = 0; k < array.size(); ++k) 21 | array.set(k,k); 22 | int sum = 0; 23 | for(int k = 0; k < array.size(); ++k) 24 | sum += array.get(k); 25 | return sum; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/SmartSummer.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | 3 | public class SmartSummer { 4 | protected Array array; 5 | 6 | public SmartSummer(Array backend) { 7 | array = backend; 8 | } 9 | 10 | 11 | public int compute() { 12 | if(array instanceof NaiveArray) { 13 | int[] back = ((NaiveArray) array).array; 14 | for(int k = 0; k < back.length; ++k) 15 | back[k] = k; 16 | int sum = 0; 17 | for(int k = 0; k < back.length; ++k) 18 | sum += back[k]; 19 | return sum; 20 | } 21 | for(int k = 0; k < array.size(); ++k) 22 | array.set(k,k); 23 | int sum = 0; 24 | for(int k = 0; k < array.size(); ++k) 25 | sum += array.get(k); 26 | return sum; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/integersum/SumBenchmark.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.integersum; 2 | import org.openjdk.jmh.annotations.Benchmark; 3 | import org.openjdk.jmh.annotations.BenchmarkMode; 4 | import org.openjdk.jmh.annotations.Mode; 5 | import org.openjdk.jmh.annotations.OutputTimeUnit; 6 | import org.openjdk.jmh.annotations.Scope; 7 | import org.openjdk.jmh.annotations.Setup; 8 | import org.openjdk.jmh.annotations.State; 9 | import org.openjdk.jmh.runner.Runner; 10 | import org.openjdk.jmh.runner.RunnerException; 11 | import org.openjdk.jmh.runner.options.Options; 12 | import org.openjdk.jmh.runner.options.OptionsBuilder; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | 16 | @State(Scope.Thread) 17 | @BenchmarkMode(Mode.AverageTime) 18 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 19 | public class SumBenchmark { 20 | 21 | ReverseFastSummer refs; 22 | FastSummer fs; 23 | BasicSummer gs; 24 | ReverseBasicSummer regs; 25 | SmartSummer ss; 26 | ReverseSmartSummer ress; 27 | SillySummer sis; 28 | FixedSummer ffs; 29 | ReverseFixedSummer reffs; 30 | 31 | @Benchmark 32 | public int RFastbench() { 33 | return refs.compute(); 34 | } 35 | 36 | @Benchmark 37 | public int Fastbench() { 38 | return fs.compute(); 39 | } 40 | 41 | @Benchmark 42 | public int Basicbench() { 43 | return gs.compute(); 44 | } 45 | 46 | @Benchmark 47 | public int RBasicbench() { 48 | return regs.compute(); 49 | } 50 | 51 | @Benchmark 52 | public int Smartbench() { 53 | return ss.compute(); 54 | } 55 | 56 | @Benchmark 57 | public int RSmartbench() { 58 | return ress.compute(); 59 | } 60 | 61 | @Benchmark 62 | public int Sillybench() { 63 | return sis.compute(); 64 | } 65 | 66 | @Benchmark 67 | public int Fixedbench() { 68 | return ffs.compute(); 69 | } 70 | 71 | @Benchmark 72 | public int RFixedbench() { 73 | return reffs.compute(); 74 | } 75 | 76 | 77 | @Setup 78 | public void setUp() { 79 | int N = 10000000; 80 | refs = new ReverseFastSummer(N); 81 | fs = new FastSummer(N); 82 | gs = new BasicSummer(new NaiveArray(N)); 83 | regs = new ReverseBasicSummer(new NaiveArray(N)); 84 | ss = new SmartSummer(new NaiveArray(N)); 85 | ress = new ReverseSmartSummer(new NaiveArray(N)); 86 | sis = new SillySummer(new NaiveArray(N)); 87 | ffs = new FixedSummer(N); 88 | reffs = new ReverseFixedSummer(N); 89 | } 90 | 91 | /* 92 | * $ mvn clean install 93 | * $ java -cp target/microbenchmarks-0.0.1.jar me.lemire.microbenchmarks.integersum.SumBenchmark -wi 5 -i 5 -f 1 94 | */ 95 | 96 | public static void main(String[] args) throws RunnerException { 97 | Options opt = new OptionsBuilder() 98 | .include(SumBenchmark.class.getSimpleName()) 99 | .warmupIterations(5) 100 | .measurementIterations(5) 101 | .forks(1) 102 | .build(); 103 | 104 | new Runner(opt).run(); 105 | } 106 | 107 | public static void oldmain(String[] args) { 108 | int N = 10000000; 109 | ReverseFastSummer refs = new ReverseFastSummer(N); 110 | FastSummer fs = new FastSummer(N); 111 | BasicSummer gs = new BasicSummer(new NaiveArray(N)); 112 | ReverseBasicSummer regs = new ReverseBasicSummer(new NaiveArray(N)); 113 | SmartSummer ss = new SmartSummer(new NaiveArray(N)); 114 | ReverseSmartSummer ress = new ReverseSmartSummer(new NaiveArray(N)); 115 | SillySummer sis = new SillySummer(new NaiveArray(N)); 116 | FixedSummer ffs = new FixedSummer(N); 117 | ReverseFixedSummer reffs = new ReverseFixedSummer(N); 118 | int bogus = 0; 119 | for(int k = 0; k < 100; ++k) { 120 | long t0 = System.nanoTime(); 121 | bogus += refs.compute(); 122 | long t1 = System.nanoTime(); 123 | bogus += fs.compute(); 124 | long t2 = System.nanoTime(); 125 | bogus += gs.compute(); 126 | long t3 = System.nanoTime(); 127 | bogus += ss.compute(); 128 | long t4 = System.nanoTime(); 129 | bogus += ress.compute(); 130 | long t5 = System.nanoTime(); 131 | bogus += sis.compute(); 132 | long t6 = System.nanoTime(); 133 | bogus += ffs.compute(); 134 | long t7 = System.nanoTime(); 135 | bogus += reffs.compute(); 136 | long t8 = System.nanoTime(); 137 | bogus += regs.compute(); 138 | long t9 = System.nanoTime(); 139 | 140 | System.out.println("refast fast basic smart resmart silly fixed rebasic"); 141 | System.out.println((t1-t0)*1.0/N+" "+(t2-t1)*1.0/N+" "+(t3-t2)*1.0/N+" "+(t4-t3)*1.0/N+" "+(t5-t4)*1.0/N+" "+(t6-t5)*1.0/N+" "+(t7-t6)*1.0/N+" "+(t8-t7)*1.0/N+" "+(t9-t8)*1.0/N); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/mysteries/MysteriousLambda.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.mysteries; 2 | 3 | import java.util.ArrayList; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.BenchmarkMode; 8 | import org.openjdk.jmh.annotations.Mode; 9 | import org.openjdk.jmh.annotations.OutputTimeUnit; 10 | import org.openjdk.jmh.annotations.Scope; 11 | import org.openjdk.jmh.annotations.State; 12 | import org.openjdk.jmh.runner.Runner; 13 | import org.openjdk.jmh.runner.RunnerException; 14 | import org.openjdk.jmh.runner.options.Options; 15 | import org.openjdk.jmh.runner.options.OptionsBuilder; 16 | 17 | @State(Scope.Thread) 18 | @BenchmarkMode(Mode.AverageTime) 19 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 20 | // http://stackoverflow.com/questions/41622613/why-is-my-java-lambda-with-a-dummy-assignment-much-faster-than-without-it 21 | public class MysteriousLambda { 22 | 23 | @State(Scope.Benchmark) 24 | public static class BenchmarkState { 25 | ArrayList fooList = new ArrayList(); 26 | 27 | public BenchmarkState() { 28 | for (int ctr = 0; ctr < 1000; ++ctr) { 29 | fooList.add(new Foo().alpha("a" + ctr).beta("b" + ctr)); 30 | } 31 | } 32 | 33 | } 34 | 35 | public static String merge(String x1, String x2) { 36 | int len1 = x1.length(); 37 | int len2 = x2.length(); 38 | char[] buffer = new char[len1 + len2]; 39 | x1.getChars(0, len1, buffer, 0); 40 | x2.getChars(0, len2, buffer, len1); 41 | return new String(buffer,0,len1 + len2); 42 | } 43 | 44 | @Benchmark 45 | public FooPrime[] basicstream(BenchmarkState s) { 46 | return (FooPrime[]) s.fooList.stream().map(it -> { 47 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 48 | }).toArray(FooPrime[]::new); 49 | } 50 | 51 | @Benchmark 52 | public FooPrime[] mergebasicstream(BenchmarkState s) { 53 | return (FooPrime[]) s.fooList.stream().map(it -> { 54 | return new FooPrime().gamma(merge(it.getAlpha(), it.getBeta())); 55 | }).toArray(FooPrime[]::new); 56 | } 57 | 58 | 59 | @Benchmark 60 | public FooPrime[] basicstreamstringbuilder(BenchmarkState s) { 61 | return (FooPrime[]) s.fooList.stream().map(it -> { 62 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 63 | }).toArray(FooPrime[]::new); 64 | } 65 | 66 | @Benchmark 67 | public FooPrime[] tweakedbasicstreamstringbuilder(BenchmarkState s) { 68 | return (FooPrime[]) s.fooList.stream().map(it -> { 69 | int stuff = it.getAlpha().length(); 70 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 71 | }).toArray(FooPrime[]::new); 72 | } 73 | 74 | @Benchmark 75 | public FooPrime[] nullbasicstreamstringbuilder(BenchmarkState s) { 76 | return (FooPrime[]) s.fooList.stream().map(it -> { 77 | if( it.getAlpha() == null) throw new NullPointerException(); 78 | return new FooPrime().gamma(new StringBuilder().append(it.getAlpha()).append(it.getBeta()).toString()); 79 | }).toArray(FooPrime[]::new); 80 | } 81 | 82 | 83 | 84 | @Benchmark 85 | public FooPrime[] basicsum(BenchmarkState s) { 86 | int howmany = s.fooList.size(); 87 | FooPrime[] answer = new FooPrime[s.fooList.size()]; 88 | for(int k = 0; k < howmany ; ++k ) { 89 | Foo x = s.fooList.get(k); 90 | answer[k] = new FooPrime(x.getAlpha() + x.getBeta()); 91 | } 92 | return answer; 93 | } 94 | @Benchmark 95 | public FooPrime[] basicsumnull(BenchmarkState s) { 96 | int howmany = s.fooList.size(); 97 | FooPrime[] answer = new FooPrime[s.fooList.size()]; 98 | for(int k = 0; k < howmany ; ++k ) { 99 | Foo x = s.fooList.get(k); 100 | if(x.getAlpha() == null) throw new NullPointerException(); 101 | answer[k] = new FooPrime(x.getAlpha() + x.getBeta()); 102 | } 103 | return answer; 104 | } 105 | 106 | 107 | 108 | @Benchmark 109 | public String[] basicsum_tostring(BenchmarkState s) { 110 | int howmany = s.fooList.size(); 111 | String[] answer = new String[s.fooList.size()]; 112 | for(int k = 0; k < howmany ; ++k ) { 113 | Foo x = s.fooList.get(k); 114 | answer[k] = x.getAlpha() + x.getBeta(); 115 | } 116 | return answer; 117 | } 118 | 119 | @Benchmark 120 | public String[] basicsumnull_tostring(BenchmarkState s) { 121 | int howmany = s.fooList.size(); 122 | String[] answer = new String[s.fooList.size()]; 123 | for(int k = 0; k < howmany ; ++k ) { 124 | Foo x = s.fooList.get(k); 125 | if(x.getAlpha() == null) throw new NullPointerException(); 126 | answer[k] = x.getAlpha() + x.getBeta(); 127 | } 128 | return answer; 129 | } 130 | 131 | 132 | 133 | @Benchmark 134 | public FooPrime[] tweakedbasicstream(BenchmarkState s) { 135 | return (FooPrime[]) s.fooList.stream().map(it -> { 136 | int stuff = it.getAlpha().length(); 137 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 138 | }).toArray(FooPrime[]::new); 139 | } 140 | 141 | @Benchmark 142 | public String[] basicstream_tostring(BenchmarkState s) { 143 | return (String[]) s.fooList.stream().map(it -> { 144 | return it.getAlpha() + it.getBeta(); 145 | }).toArray(String[]::new); 146 | } 147 | 148 | 149 | @Benchmark 150 | public String[] tweakedbasicstream_tostring(BenchmarkState s) { 151 | return (String[]) s.fooList.stream().map(it -> { 152 | int stuff = it.getAlpha().length(); 153 | return it.getAlpha() + it.getBeta(); 154 | }).toArray(String[]::new); 155 | } 156 | 157 | 158 | @Benchmark 159 | public FooPrime[] nullbasicstream(BenchmarkState s) { 160 | return (FooPrime[]) s.fooList.stream().map(it -> { 161 | if( it.getAlpha() == null) throw new NullPointerException(); 162 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 163 | }).toArray(FooPrime[]::new); 164 | } 165 | 166 | 167 | 168 | @Benchmark 169 | public FooPrime[] betanullbasicstream(BenchmarkState s) { 170 | return (FooPrime[]) s.fooList.stream().map(it -> { 171 | if( it.getBeta() == null) throw new NullPointerException(); 172 | return new FooPrime().gamma(it.getAlpha() + it.getBeta()); 173 | }).toArray(FooPrime[]::new); 174 | } 175 | 176 | 177 | public static void main(String[] args) throws RunnerException { 178 | Options opt = new OptionsBuilder() 179 | .include(MysteriousLambda.class.getSimpleName()).warmupIterations(5) 180 | .measurementIterations(5).forks(1).build(); 181 | 182 | new Runner(opt).run(); 183 | } 184 | 185 | } 186 | 187 | final class Foo { 188 | private String alpha; 189 | private String beta; 190 | 191 | public String getAlpha() { 192 | return alpha; 193 | } 194 | 195 | public String getBeta() { 196 | return beta; 197 | } 198 | 199 | public void setAlpha(String alpha) { 200 | this.alpha = alpha; 201 | } 202 | 203 | public void setBeta(String beta) { 204 | this.beta = beta; 205 | } 206 | 207 | public Foo alpha(String alpha) { 208 | this.alpha = alpha; 209 | return this; 210 | } 211 | 212 | public Foo beta(String beta) { 213 | this.beta = beta; 214 | return this; 215 | } 216 | } 217 | 218 | final class FooPrime { 219 | private String gamma; 220 | 221 | public FooPrime() { 222 | } 223 | 224 | public FooPrime(String x) { 225 | gamma = x; 226 | } 227 | 228 | public String getGamma() { 229 | return gamma; 230 | } 231 | 232 | public void setGamma(String gamma) { 233 | this.gamma = gamma; 234 | } 235 | 236 | public FooPrime gamma(String gamma) { 237 | this.gamma = gamma; 238 | return this; 239 | } 240 | 241 | @Override 242 | public int hashCode() { 243 | final int prime = 31; 244 | int result = 1; 245 | result = prime * result + ((gamma == null) ? 0 : gamma.hashCode()); 246 | return result; 247 | } 248 | 249 | @Override 250 | public boolean equals(Object obj) { 251 | if (this == obj) 252 | return true; 253 | if (obj == null) 254 | return false; 255 | if (getClass() != obj.getClass()) 256 | return false; 257 | FooPrime other = (FooPrime) obj; 258 | if (gamma == null) { 259 | if (other.gamma != null) 260 | return false; 261 | } else if (!gamma.equals(other.gamma)) 262 | return false; 263 | return true; 264 | } 265 | 266 | @Override 267 | public String toString() { 268 | return "FooPrime [gamma=" + gamma + "]"; 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/mysteries/MysteriousLambdaJustCopy.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.mysteries; 2 | 3 | import java.util.ArrayList; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.BenchmarkMode; 8 | import org.openjdk.jmh.annotations.Mode; 9 | import org.openjdk.jmh.annotations.OutputTimeUnit; 10 | import org.openjdk.jmh.annotations.Scope; 11 | import org.openjdk.jmh.annotations.State; 12 | import org.openjdk.jmh.runner.Runner; 13 | import org.openjdk.jmh.runner.RunnerException; 14 | import org.openjdk.jmh.runner.options.Options; 15 | import org.openjdk.jmh.runner.options.OptionsBuilder; 16 | 17 | @State(Scope.Thread) 18 | @BenchmarkMode(Mode.AverageTime) 19 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 20 | // http://stackoverflow.com/questions/41622613/why-is-my-java-lambda-with-a-dummy-assignment-much-faster-than-without-it 21 | public class MysteriousLambdaJustCopy { 22 | 23 | @State(Scope.Benchmark) 24 | public static class BenchmarkState { 25 | ArrayList fooList = new ArrayList(); 26 | 27 | public BenchmarkState() { 28 | for (int ctr = 0; ctr < 1000; ++ctr) { 29 | fooList.add(new Foo().alpha("a" + ctr).beta("b" + ctr)); 30 | } 31 | } 32 | 33 | } 34 | 35 | 36 | @Benchmark 37 | public FooPrime[] basicstream(BenchmarkState s) { 38 | return (FooPrime[]) s.fooList.stream().map(it -> { 39 | return new FooPrime().gamma(it.getAlpha()); 40 | }).toArray(FooPrime[]::new); 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | @Benchmark 49 | public FooPrime[] tweakedbasicstream(BenchmarkState s) { 50 | return (FooPrime[]) s.fooList.stream().map(it -> { 51 | int stuff = it.getAlpha().length(); 52 | return new FooPrime().gamma(it.getAlpha()); 53 | }).toArray(FooPrime[]::new); 54 | } 55 | 56 | @Benchmark 57 | public FooPrime[] nullbasicstream(BenchmarkState s) { 58 | return (FooPrime[]) s.fooList.stream().map(it -> { 59 | if( it.getAlpha() == null) throw new NullPointerException(); 60 | return new FooPrime().gamma(it.getAlpha()); 61 | }).toArray(FooPrime[]::new); 62 | } 63 | 64 | 65 | 66 | @Benchmark 67 | public FooPrime[] betanullbasicstream(BenchmarkState s) { 68 | return (FooPrime[]) s.fooList.stream().map(it -> { 69 | if( it.getBeta() == null) throw new NullPointerException(); 70 | return new FooPrime().gamma(it.getAlpha()); 71 | }).toArray(FooPrime[]::new); 72 | } 73 | 74 | 75 | public static void main(String[] args) throws RunnerException { 76 | Options opt = new OptionsBuilder() 77 | .include(MysteriousLambdaJustCopy.class.getSimpleName()).warmupIterations(5) 78 | .measurementIterations(5).forks(1).build(); 79 | 80 | new Runner(opt).run(); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/mysteries/StringMerge.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.mysteries; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | import org.openjdk.jmh.annotations.*; 5 | import org.openjdk.jmh.runner.*; 6 | import org.openjdk.jmh.runner.options.*; 7 | 8 | @State(Scope.Thread) 9 | @BenchmarkMode(Mode.AverageTime) 10 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 11 | public class StringMerge { 12 | 13 | 14 | @State(Scope.Benchmark) 15 | public static class BenchmarkState { 16 | final int N = 1000; 17 | String[] list1 = new String[N]; 18 | String[] list2 = new String[N]; 19 | String[] list3 = new String[N]; 20 | 21 | public BenchmarkState() { 22 | for (int ctr = 0; ctr < N; ++ctr) { 23 | list1[ctr] = "a" + ctr; 24 | list2[ctr] = "a" + ctr; 25 | } 26 | } 27 | } 28 | 29 | @Benchmark 30 | public void stringsum(BenchmarkState s) { 31 | for(int k = 0; k < s.N; ++k) s.list3[k] = s.list1[k] + s.list2[k]; 32 | } 33 | 34 | 35 | @Benchmark 36 | public void stringsum_withexcept(BenchmarkState s) { 37 | for(int k = 0; k < s.N; ++k) { 38 | if(s.list1[k] == null) throw new NullPointerException(); 39 | s.list3[k] = s.list1[k] + s.list2[k]; 40 | } 41 | } 42 | 43 | public static void main(String[] args) throws RunnerException { 44 | Options opt = new OptionsBuilder() 45 | .include(StringMerge.class.getSimpleName()).warmupIterations(5) 46 | .measurementIterations(5).forks(1).build(); 47 | new Runner(opt).run(); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/random/Division.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.random; 2 | 3 | 4 | import org.openjdk.jmh.annotations.Benchmark; 5 | import org.openjdk.jmh.annotations.Scope; 6 | import org.openjdk.jmh.annotations.State; 7 | import org.openjdk.jmh.runner.Runner; 8 | import org.openjdk.jmh.runner.RunnerException; 9 | import org.openjdk.jmh.runner.options.Options; 10 | import org.openjdk.jmh.runner.options.OptionsBuilder; 11 | 12 | 13 | 14 | public class Division { 15 | 16 | 17 | @State(Scope.Benchmark) 18 | public static class BenchmarkState { 19 | final float divisor = (float) (1<<14); 20 | final float precomputeddivisor = 1 / divisor; 21 | final float divisor3 = (float) (3); 22 | final float precomputeddivisor3 = 1 / divisor3; 23 | int length = 10; 24 | } 25 | 26 | 27 | @Benchmark 28 | public float precompDivision(BenchmarkState s) { 29 | float sum = 0; 30 | for(int k = 0; k < s.length; ++k) 31 | sum += s.precomputeddivisor * k; 32 | return sum; 33 | } 34 | 35 | @Benchmark 36 | public float division(BenchmarkState s) { 37 | float sum = 0; 38 | for(int k = 0; k < s.length; ++k) 39 | sum += k / s.divisor; 40 | return sum; 41 | } 42 | 43 | @Benchmark 44 | public float divisionBy3(BenchmarkState s) { 45 | float sum = 0; 46 | for(int k = 0; k < s.length; ++k) 47 | sum += k / (float) (3); 48 | return sum; 49 | } 50 | 51 | @Benchmark 52 | public float divisionBy3ThroughMultiplication(BenchmarkState s) { 53 | float sum = 0; 54 | for(int k = 0; k < s.length; ++k) 55 | sum += k * s.precomputeddivisor3; 56 | return sum; 57 | } 58 | 59 | 60 | public static void main(String[] args) throws RunnerException { 61 | Options opt = new OptionsBuilder() 62 | .include(Division.class.getSimpleName()) 63 | .warmupIterations(5).measurementIterations(5).forks(1).build(); 64 | 65 | new Runner(opt).run(); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/random/RandomNumberGenerator.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.random; 2 | 3 | import java.util.Random; 4 | import java.util.concurrent.ThreadLocalRandom; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import com.github.alexeyr.pcg.Pcg32; 8 | 9 | import org.openjdk.jmh.annotations.Benchmark; 10 | import org.openjdk.jmh.annotations.BenchmarkMode; 11 | import org.openjdk.jmh.annotations.Mode; 12 | import org.openjdk.jmh.annotations.OutputTimeUnit; 13 | import org.openjdk.jmh.annotations.Scope; 14 | import org.openjdk.jmh.annotations.State; 15 | import org.openjdk.jmh.runner.Runner; 16 | import org.openjdk.jmh.runner.RunnerException; 17 | import org.openjdk.jmh.runner.options.Options; 18 | import org.openjdk.jmh.runner.options.OptionsBuilder; 19 | 20 | @State(Scope.Thread) 21 | @BenchmarkMode(Mode.AverageTime) 22 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 23 | public class RandomNumberGenerator { 24 | 25 | 26 | @State(Scope.Benchmark) 27 | public static class BenchmarkState { 28 | Random r = new Random(); 29 | long seed = System.nanoTime(); 30 | Pcg32 rnd = new Pcg32(); 31 | 32 | public int manualJavaNext(int bits) { 33 | seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); 34 | return (int) (seed >>> (48 - bits)); 35 | } 36 | } 37 | 38 | 39 | @Benchmark 40 | public int basicJavaRandom(BenchmarkState s) { 41 | return s.r.nextInt(); 42 | } 43 | 44 | @Benchmark 45 | public int basicJavaRandomRanged1000(BenchmarkState s) { 46 | return s.r.nextInt(1000); 47 | } 48 | 49 | 50 | @Benchmark 51 | public int manualJavaRandom(BenchmarkState s) { 52 | return s.manualJavaNext(32); 53 | } 54 | 55 | @Benchmark 56 | public int basicThreadLocalRandom(BenchmarkState s) { 57 | return ThreadLocalRandom.current().nextInt(); 58 | } 59 | 60 | 61 | @Benchmark 62 | public int basicThreadLocalRandomRanged1000(BenchmarkState s) { 63 | return ThreadLocalRandom.current().nextInt(1000); 64 | } 65 | 66 | @Benchmark 67 | public int pcgJavaRandom(BenchmarkState s) { 68 | return s.rnd.nextInt(); 69 | } 70 | 71 | 72 | @Benchmark 73 | public int pcgJavaRandomRanged1000(BenchmarkState s) { 74 | return s.rnd.nextInt(1000); 75 | } 76 | 77 | 78 | 79 | public static void main(String[] args) throws RunnerException { 80 | Options opt = new OptionsBuilder() 81 | .include(RandomNumberGenerator.class.getSimpleName()).warmupIterations(2) 82 | .measurementIterations(3).forks(1).build(); 83 | 84 | new Runner(opt).run(); 85 | } 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /src/main/java/me/lemire/microbenchmarks/rangequery/RangeMinimum.java: -------------------------------------------------------------------------------- 1 | package me.lemire.microbenchmarks.rangequery; 2 | 3 | import java.util.Random; 4 | 5 | import org.openjdk.jmh.annotations.*; 6 | import org.openjdk.jmh.runner.Runner; 7 | import java.util.concurrent.TimeUnit; 8 | import org.openjdk.jmh.runner.RunnerException; 9 | import org.openjdk.jmh.runner.options.Options; 10 | import org.openjdk.jmh.runner.options.OptionsBuilder; 11 | 12 | 13 | 14 | @State(Scope.Thread) 15 | @BenchmarkMode(Mode.AverageTime) 16 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 17 | public class RangeMinimum { 18 | final static int N = 10000; 19 | final static int t = 100; 20 | static int[] b = new int[N]; 21 | static int[] m = new int[t]; 22 | static int[] M = new int[t]; 23 | 24 | @Setup 25 | public void populate() { 26 | System.out.println("populating..."); 27 | Random r = new Random(); 28 | for (int i = 0; i < N; i++) { 29 | b[i] = r.nextInt(); 30 | } 31 | for (int i = 0; i < t; i++) { 32 | m[i] = r.nextInt(N - 1); 33 | M[i] = r.nextInt(N - m[i] - 1) + m[i] + 1; 34 | } 35 | System.out.println("populating... ok"); 36 | 37 | } 38 | 39 | 40 | @Benchmark 41 | public int embeddedmin() { 42 | int sum = 0; 43 | for (int i = 0; i < t; i++) { 44 | final int minrange = m[i]; 45 | final int maxrange = M[i]; 46 | int thismin = b[minrange]; 47 | for(int j = minrange + 1; j < maxrange; j ++) { 48 | if(thismin < b[j]) thismin = b[j]; 49 | } 50 | sum += thismin; 51 | } 52 | return sum; 53 | } 54 | 55 | @Benchmark 56 | public int fncmin() { 57 | int sum = 0; 58 | for (int i = 0; i < t; i++) { 59 | sum += RangeMinimum.rangemin(b,m[i], M[i]); 60 | } 61 | return sum; 62 | } 63 | 64 | static int rangemin(int[] b, int minrange, int maxrange) { 65 | int thismin = b[minrange]; 66 | for(int j = minrange + 1; j < maxrange; j ++) { 67 | if(thismin < b[j]) thismin = b[j]; 68 | } 69 | return thismin; 70 | } 71 | 72 | public static void main(String[] args) throws RunnerException { 73 | Options opt = new OptionsBuilder() 74 | .include(RangeMinimum.class.getSimpleName()) 75 | .warmupIterations(5).measurementIterations(5).forks(1).build(); 76 | 77 | new Runner(opt).run(); 78 | } 79 | 80 | } 81 | --------------------------------------------------------------------------------