Wait, foreach is slower then for loop? I did not know this.
I completed 100 Days of Java over 5 years and mapped the journey as a graph
21–30 of 36 posts
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#22Just use Apache Commons Lang Strings.CS.equalsAny . If not, List.of or Set.of (if N could be big) should be the best options. Streams are better when you chain many operations.
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#23Wait, foreach is slower then for loop? I did not know this.
import java.util.ArrayList;
import java.util.List;
class Day06 {
public static void main(String args[]) {
List fileTypeList = new ArrayList();
for (int i = 0; i
Empty loops and no warmup (at a minimum!) make for a somewhat suboptimal benchmark, to say the least. To be honest I'm surprised the JIT didn't eliminate the loops altogether.If you want proper results you probably want to use the Java Microbenchmark Harness [0]. You'd probably want some actual data/work as well so the JIT doesn't overspecialize on the benchmark.
Edit: Halfheartedly tried to adapt the LinkedIn benchmark to JMH. Still not a great benchmark and I'm rusty so I wouldn't be surprised if I messed something up, but it's hopefully better the original:
package org.sample;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {
List fileTypeList = new ArrayList() {{
for (int i = 0; i
And the result summary (run on Ubuntu via WSL2): # JMH version: 1.37
# VM version: JDK 21.0.10, OpenJDK 64-Bit Server VM, 21.0.10+7-Ubuntu-124.04
# VM invoker: /usr/lib/jvm/java-21-openjdk-amd64/bin/java
# VM options:
# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
Result "org.sample.MyBenchmark.baseline":
0.254 ±(99.9%) 0.022 ns/op [Average]
(min, avg, max) = (0.247, 0.254, 0.262), stdev = 0.006
CI (99.9%): [0.232, 0.276] (assumes normal distribution)
Result "org.sample.MyBenchmark.measureFor":
693178.390 ±(99.9%) 60793.583 ns/op [Average]
(min, avg, max) = (676266.480, 693178.390, 718114.554), stdev = 15787.901
CI (99.9%): [632384.806, 753971.973] (assumes normal distribution)
Result "org.sample.MyBenchmark.measureForEach":
693756.240 ±(99.9%) 7549.769 ns/op [Average]
(min, avg, max) = (691685.231, 693756.240, 696573.323), stdev = 1960.651
CI (99.9%): [686206.470, 701306.009] (assumes normal distribution)
Doesn't look like a significant difference to me, though obviously the benchmark quality leaves something to be desired.
[0]: https://github.com/openjdk/jmhRe: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#24Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#25This website is very confusing.
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#26Earlier quoted context omitted.
I taught students in Java and Python and noticed that Python is actually harder. Things you don't even notice, like breathing, make a lot of confusion for newcomers. The biggest one is types -- students think in terms of meaning, not types, e.g. "this my variable contains first name", but they don't realize it's a string. Or "this is products in checkout cart", but the variable is an integer, so it's products count,…
Java would be fine if it and it's community wouldn't force OOP and GOF patterns everywhere.
Modern Java can be OOP, functional or procedural where useful. GoF patterns were never enforced by the language.
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#27Earlier quoted context omitted.
Java would be fine if it and it's community wouldn't force OOP and GOF patterns everywhere.
2003 called and wants its arguments against Java back. Modern Java can be OOP, functional or procedural where useful. GoF patterns were never enforced by the language.
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#28Earlier quoted context omitted.
Java would be fine if it and it's community wouldn't force OOP and GOF patterns everywhere.
2003 called and wants its arguments against Java back. Modern Java can be OOP, functional or procedural where useful. GoF patterns were never enforced by the language.
Or maybe that's even a bit rude to GoF. Some OO patterns - pretty questionable ones - were pushed pretty hard especially in Java EE.
Re: I completed 100 Days of Java over 5 years and mapped the journey as a graph
#29Earlier quoted context omitted.
Java would be fine if it and it's community wouldn't force OOP and GOF patterns everywhere.
2003 called and wants its arguments against Java back. Modern Java can be OOP, functional or procedural where useful. GoF patterns were never enforced by the language.