Code Monkey home page Code Monkey logo

Comments (7)

oussamaLaribi avatar oussamaLaribi commented on May 25, 2024 1

Hi @dedece35,
thank you for ideas. I agree with you to benchmark other kind of loops.

from ecocode.

olegoaer avatar olegoaer commented on May 25, 2024

I don't know where this rule comes from but it's for sure not part of the Android-specific plugin

from ecocode.

oussamaLaribi avatar oussamaLaribi commented on May 25, 2024

I think this rule has to do with Java performance with Collections. As i know, Foreach and Stream API is convenient to work with Collections.

from ecocode.

dedece35 avatar dedece35 commented on May 25, 2024

Hi @oussamaLaribi ,

I think @durandx would like to have an evidence to confirm what you said. I agree with @durandx. If we don't have a paper, it would b great to do benchmark test to check it (for example with JMH framework for example)
But this discussion goes is a general discussion : how to prove that each implemented rule is legitimate.
what's your opinion @olegoaer, @jules-delecour-dav, @glalloue, @mdubois81 ?

from ecocode.

oussamaLaribi avatar oussamaLaribi commented on May 25, 2024

Hello @dedece35, I have made a benchmark to test the example in the description of the rule. Here’s the code and the result. I'm kinda surprised.
Benchmark

ElapsedTime
https://github.com/oussamaLaribi/jmh-benchmark-JAVA-S53-rule

from ecocode.

dedece35 avatar dedece35 commented on May 25, 2024

Hi @oussamaLaribi ,

ok. good for micro-benchmark with JMH !
but In your code, there are some useless code. The unit test is useless for me, because benchmark is enough to prove it.

Another idea, is to benchmark other kind of loops. For example, to benchmark a stream, or standard for loop with simple index.

from ecocode.

skerdudou avatar skerdudou commented on May 25, 2024

Thank you @oussamaLaribi for the benchmark. So if I understand correctly, looping from an Array is quicker than from a List, the exact opposite that said by the S53 rule !

To be sure, I made some more tests with JMH.

I added a test where the Arrays.asList is not prebaked (as I would "correct" my code regarding this rule).
I also added a .stream().forEach as suggested (I'm curious too 😄).

With a big list (1_000_000 items) :

Benchmark                      Mode  Cnt     Score     Error  Units
BenchmarkForEach.forEachArray  avgt    5  2440,479 ±  24,270  us/op
BenchmarkForEach.forEachList   avgt    5  4681,401 ±  85,861  us/op
BenchmarkForEach.forEachList2  avgt    5  4722,169 ±  99,151  us/op
BenchmarkForEach.stream        avgt    5  4403,399 ± 329,694  us/op

With a small list (10 items) :

Benchmark                      Mode  Cnt  Score   Error  Units
BenchmarkForEach.forEachArray  avgt    5  0,027 ± 0,002  us/op
BenchmarkForEach.forEachList   avgt    5  0,038 ± 0,002  us/op
BenchmarkForEach.forEachList2  avgt    5  0,042 ± 0,003  us/op
BenchmarkForEach.stream        avgt    5  0,038 ± 0,002  us/op

With these tests I constantly have better performance on arrays...

from ecocode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.