<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: paxel</title>
    <description>The latest articles on DEV Community by paxel (@paxel).</description>
    <link>https://dev.to/paxel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F449362%2Ffcbf3cd0-b945-44e0-8fa2-95bd26b1d00d.jpeg</url>
      <title>DEV Community: paxel</title>
      <link>https://dev.to/paxel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paxel"/>
    <language>en</language>
    <item>
      <title>Black White Deny Block Accept List</title>
      <dc:creator>paxel</dc:creator>
      <pubDate>Wed, 29 Nov 2023 14:28:40 +0000</pubDate>
      <link>https://dev.to/paxel/black-white-deny-block-accept-list-3hk0</link>
      <guid>https://dev.to/paxel/black-white-deny-block-accept-list-3hk0</guid>
      <description>&lt;p&gt;I am totally ok with getting rid of master, slave, blacklist, whitelist and such. But it seems there is a lack of agreement on what to use instead. &lt;em&gt;Blocklist&lt;/em&gt;, &lt;em&gt;DenyList&lt;/em&gt;, &lt;em&gt;AllowList&lt;/em&gt;, &lt;em&gt;AcceptList _and _Welcome-List&lt;/em&gt; are all not a good fit, the world generally agrees.&lt;/p&gt;

&lt;p&gt;The obvious solution &lt;strong&gt;Pirate-&lt;/strong&gt; and &lt;strong&gt;NinjaList&lt;/strong&gt; is also not welcomed for unknown reasons.&lt;/p&gt;

&lt;p&gt;So I decide here for all of us now:&lt;br&gt;
BlackList is now &lt;strong&gt;NopeList&lt;/strong&gt;&lt;br&gt;
WhiteList is now &lt;strong&gt;AyeList&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Case closed. See you at the bar&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bABWXfZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd2m9l9a7je418axsmi4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bABWXfZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd2m9l9a7je418axsmi4.jpg" alt="Image description" width="800" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rant</category>
    </item>
    <item>
      <title>A Java myth busted (or is it?)</title>
      <dc:creator>paxel</dc:creator>
      <pubDate>Sun, 07 Aug 2022 20:21:00 +0000</pubDate>
      <link>https://dev.to/paxel/a-java-myth-busted-or-is-it-27l3</link>
      <guid>https://dev.to/paxel/a-java-myth-busted-or-is-it-27l3</guid>
      <description>&lt;p&gt;tl;dr &lt;strong&gt;for-loops are not necessary way more performant than stream()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;when in JDK 8 stream() was introduced to the java Collections, the immediate reaction was: "they are sooooo &lt;br&gt;
much slower than a normal for-loop".&lt;br&gt;
I tested it and indeed it was slower. by alot. &lt;em&gt;(I remember at some point that oracle admitted that it is slower, but the focus was on functionality and the performance would probably improve in the future. alas I can not find a source for my memory, so let's assume it is wrong.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;what definetly exists are lots of articles about how slow streams are compared to for-loops. e.g. &lt;a href="https://nipafx.dev/java-stream-performance/"&gt;nipafx&lt;/a&gt;, who proved it with JMH and &lt;a href="https://devm.io/java/java-performance-tutorial-how-fast-are-the-java-8-streams-118830"&gt;Angelika&lt;/a&gt; with the compelling argument, that the compiler optimization for loops is too good to be beaten by streams.&lt;/p&gt;

&lt;p&gt;some developers took this fact and kept it stored in their brain forever. but streams were introduced 2014. 8 years have passed. how does it look today? is it really as slow as some repeatedly declare? let's find out.&lt;/p&gt;

&lt;p&gt;I wrote a set of &lt;a href="https://github.com/paxel/group-executor/blob/playground/src/test/java/paxel/bulkexecutor/JmhStreamPerformanceMeasurement.java"&gt;benchmarks&lt;/a&gt; that (to my knowledge) use the correct procedure in JMH.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create the data in a @State object&lt;/li&gt;
&lt;li&gt;destroy the result in a blackhole&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I let it process loops of 10, 10_000 and 10_000_000 entries. and these are the results:&lt;/p&gt;

&lt;h2&gt;
  
  
  10 entries
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Benchmark                                               Mode  Cnt       Score       Error  Units
JmhStreamPerformanceMeasurement.collectFilteredFor     thrpt   25  691000.985 ±  5338.170  ops/s
JmhStreamPerformanceMeasurement.collectFilteredForGet  thrpt   25  687244.094 ±  2287.375  ops/s
JmhStreamPerformanceMeasurement.collectFilteredStream  thrpt   25  620127.959 ± 11149.611  ops/s

JmhStreamPerformanceMeasurement.collectFor             thrpt   25  601047.148 ±  6901.828  ops/s
JmhStreamPerformanceMeasurement.collectForGet          thrpt   25  593137.918 ±  7027.976  ops/s
JmhStreamPerformanceMeasurement.collectStream          thrpt   25  583345.516 ±  2706.945  ops/s

JmhStreamPerformanceMeasurement.easyTaskFor            thrpt   25  752205.384 ±  3155.479  ops/s
JmhStreamPerformanceMeasurement.easyTaskForGet         thrpt   25  753751.877 ±  2618.748  ops/s
JmhStreamPerformanceMeasurement.easyTaskStream         thrpt   25  732847.868 ±  1268.374  ops/s

JmhStreamPerformanceMeasurement.heavyTaskFor           thrpt   25  725538.827 ±   859.032  ops/s
JmhStreamPerformanceMeasurement.heavyTaskForGet        thrpt   25  725200.238 ±   825.300  ops/s
JmhStreamPerformanceMeasurement.heavyTaskStream        thrpt   25  723650.793 ±  1007.079  ops/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10_000 entries
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Benchmark                                               Mode  Cnt      Score     Error  Units
JmhStreamPerformanceMeasurement.collectFilteredFor     thrpt   25   4700.019 ±  13.206  ops/s
JmhStreamPerformanceMeasurement.collectFilteredForGet  thrpt   25   4613.177 ±  52.664  ops/s
JmhStreamPerformanceMeasurement.collectFilteredStream  thrpt   25   4718.937 ± 232.897  ops/s

JmhStreamPerformanceMeasurement.collectFor             thrpt   25   1369.088 ±  10.711  ops/s
JmhStreamPerformanceMeasurement.collectForGet          thrpt   25   1337.578 ±  10.015  ops/s
JmhStreamPerformanceMeasurement.collectStream          thrpt   25   1383.158 ±  49.265  ops/s

JmhStreamPerformanceMeasurement.easyTaskFor            thrpt   25  39043.233 ± 708.907  ops/s
JmhStreamPerformanceMeasurement.easyTaskForGet         thrpt   25  42027.702 ±  91.457  ops/s
JmhStreamPerformanceMeasurement.easyTaskStream         thrpt   25  40108.355 ± 123.484  ops/s

JmhStreamPerformanceMeasurement.heavyTaskFor           thrpt   25   9309.883 ±  13.252  ops/s
JmhStreamPerformanceMeasurement.heavyTaskForGet        thrpt   25  14033.988 ±  13.011  ops/s
JmhStreamPerformanceMeasurement.heavyTaskStream        thrpt   25  13440.062 ±  98.916  ops/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10_000_000 entries
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Benchmark                                               Mode  Cnt   Score   Error  Units
JmhStreamPerformanceMeasurement.collectFilteredFor     thrpt   25   1.256 ± 0.044  ops/s
JmhStreamPerformanceMeasurement.collectFilteredForGet  thrpt   25   1.240 ± 0.038  ops/s
JmhStreamPerformanceMeasurement.collectFilteredStream  thrpt   25   1.182 ± 0.052  ops/s

JmhStreamPerformanceMeasurement.collectFor             thrpt   25   0.321 ± 0.006  ops/s
JmhStreamPerformanceMeasurement.collectForGet          thrpt   25   0.324 ± 0.005  ops/s
JmhStreamPerformanceMeasurement.collectStream          thrpt   25   0.322 ± 0.006  ops/s

JmhStreamPerformanceMeasurement.easyTaskFor            thrpt   25  39.874 ± 0.326  ops/s
JmhStreamPerformanceMeasurement.easyTaskForGet         thrpt   25  40.546 ± 0.356  ops/s
JmhStreamPerformanceMeasurement.easyTaskStream         thrpt   25  40.263 ± 0.374  ops/s

JmhStreamPerformanceMeasurement.heavyTaskFor           thrpt   25  14.993 ± 0.083  ops/s
JmhStreamPerformanceMeasurement.heavyTaskForGet        thrpt   25  14.795 ± 0.091  ops/s
JmhStreamPerformanceMeasurement.heavyTaskStream        thrpt   25  14.746 ± 0.076  ops/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;the benchmarks ran for 6 hours and ironed out most of the peaks.&lt;br&gt;
the result is in operations per second, so the bigger the better.&lt;/p&gt;

&lt;p&gt;in case you're too lazy to look what the benchmarks mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;For&lt;/code&gt; is a normal modern for-loop &lt;code&gt;for (X x:xs)&lt;/code&gt; that uses an iterator to run over the entries.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ForGet&lt;/code&gt; is a an old-school &lt;code&gt;for (int i = 0; i &amp;lt; xs.size();x++)&lt;/code&gt; that calls then &lt;code&gt;get(i)&lt;/code&gt; on an ArrayList&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Stream&lt;/code&gt; is the modern stream() variant.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collect&lt;/code&gt; adds all entities of the list to a set.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CollectFiltered&lt;/code&gt; adds only selected values to the set&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EasyTask&lt;/code&gt; sums up all the entries&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HeavyTask&lt;/code&gt; does a bit more if else and math stuff with the entries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  My expectations
&lt;/h3&gt;

&lt;p&gt;I would guess that the &lt;code&gt;ForGet&lt;/code&gt; benchmarks will be the fastest one of the three, because there will be no Iterator generated and &lt;code&gt;get(i)&lt;/code&gt; on ArrayList is basically only a wrapped array access.&lt;/p&gt;

&lt;p&gt;I would also assume that the &lt;code&gt;For&lt;/code&gt; is faster than the &lt;code&gt;Stream&lt;/code&gt; because it only generates one more Iterator instance, while &lt;code&gt;stream()&lt;/code&gt; generates a bunch of instances to process the data. &lt;/p&gt;

&lt;p&gt;I also assume that this overhead will go away with longer loops. One instance vs 10 instance on 10 million iterations is neglectable.&lt;/p&gt;

&lt;h3&gt;
  
  
  The result
&lt;/h3&gt;

&lt;p&gt;The data looks almost as expected, except that &lt;code&gt;stream()&lt;/code&gt; does not at all look like always the slowest. Feel free to check my benchmark code and maybe I did a mistake.&lt;/p&gt;

&lt;p&gt;It looks that with short loops (few entries) the stream is up to 11% slower than a for-loop. but it depends very much on what you execute. the easyTask is the worst. the filteredCollect also not looking good for 10 entries.&lt;br&gt;
but this changes already with 10_000 entries: then filteredCollect with stream is the fastest. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SOOOO&lt;/strong&gt; I think the difference between the three measured loops is irrelevant. &lt;br&gt;
I don't think that any of them is "way faster".&lt;br&gt;
all three work very different, some have more overhead, but might be more intelligent, but none of them will be the bottleneck in any way.&lt;/p&gt;

&lt;p&gt;some numbers seem odd and therefore I ran the benchmark twice to eliminate background processes interfering with the result.&lt;/p&gt;

&lt;p&gt;Rule of thumb maybe: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;short simple tasks probably better a for-loop.&lt;/li&gt;
&lt;li&gt;long complex tasks probably better a stream()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;as soon as you have to handle exceptions the for-loop is better anyway, because that is terrible in stream()&lt;/p&gt;

&lt;p&gt;There is the one benchmark that looks suspicious: heavyTaskFor with 10_000 entries. I will repeat that again and comment on it. I assume my machine did something weird at that time. ignore it please for now&lt;/p&gt;

&lt;p&gt;cheers, thanks for reading.&lt;/p&gt;

</description>
      <category>java</category>
      <category>performance</category>
      <category>jmh</category>
      <category>benchmark</category>
    </item>
  </channel>
</rss>
