I am trying to benchmark two different implementations of a Java function that checks whether a number contains at least one even digit. The first implementation uses hand written loop, while the second uses a stream.  public class StreamTest {  public boolean containsEvenDigit(int n) {     char [] digits = String.valueOf(n).toCharArray();     for(char d : digits) {         if(d % 2 == 0) {             return true;         }     }     return false; }  public boolean containsEvenDigitStreams(int n) {     return String.valueOf(n).chars().anyMatch(i -&gt; i % 2 == 0); }  public static void main(String[] args) {     System.out.println("============WITHOUT STREAM============");     long start1 = System.nanoTime();     for(int i = 0 ; i &lt; 1_000_000; i++) {         (new StreamTest()).containsEvenDigit(11215 + i);     }     long duration1 = (long)(System.nanoTime() - start1)/1_000_000;     System.out.println("Duration : " + duration1 + " milliseconds.");     System.out.println();      System.out.println("============USING STREAM============");     long start2 = System.nanoTime();     for(int i = 0 ; i &lt; 1_000_000; i++) {         (new StreamTest()).containsEvenDigitStreams(11215 + i);     }     long duration2 = (long)(System.nanoTime() - start2)/1_000_000;     System.out.println("Duration : " + duration2 + " milliseconds.");     System.out.println();  }   }  In the main function, I am not outputting the return values of the functions being tested to eliminate I/O overhead. The outcome shows that the implementation using streams performs much slower than the ones using loop.  ============WITHOUT STREAM============ Duration : 119 milliseconds.  ============USING STREAM============ Duration : 771 milliseconds.   Does this mean that Java 8 streams are slower than hand written loops?   Update: I am now using JMH to benchmark the two functions.   public class MyBenchmark {  private boolean containsEvenDigit(int n) {     char [] digits = String.valueOf(n).toCharArray();     for(char d : digits) {         if(d % 2 == 0) {             return true;         }     }     return false; }  private boolean containsEvenDigitStream(int n) {     return String.valueOf(n).chars().anyMatch(i -&gt; i % 2 == 0); }  @State(Scope.Thread) public static class MyState{     public int x = 1_357_997_531; }  @Benchmark public void testLoop(MyState state, Blackhole blackhole) {     boolean retVal = containsEvenDigit(state.x);     blackhole.consume(retVal); }  @Benchmark public void testStream(MyState state, Blackhole blackhole) {     boolean retVal = containsEvenDigitStream(state.x);     blackhole.consume(retVal); }  public static void main(String[] args) throws RunnerException {     Options opt = new OptionsBuilder()             .include(MyBenchmark.class.getSimpleName())             .forks(1)             .build();      new Runner(opt).run(); }   }  This time too the hand written loop beats the stream by an order of about 8. Here is the result summary.  Benchmark                Mode  Cnt        Score        Error  Units MyBenchmark.testLoop    thrpt  200  3578620.170 ± 207106.919  ops/s MyBenchmark.testStream  thrpt  200   433884.589 ±  23993.270  ops/s
How do you write (and run) a correct micro-benchmark in Java?  I'm looking for some code samples and comments illustrating various things to think about.  Example: Should the benchmark measure time/iteration or iterations/time, and why?  Related:
I have a British passport from 2006 September and going to Florida. The passport in has camera symbol. Is it valid? I am going to Florida in a few days. I have a British passport that I acquired in September 2006. It has a "camera" symbol on front and is biometric. Just seen news items that only passports from October 2006 or later will be accepted by the USA. Is my passport valid for travel to the USA? I do have a valid ESTA. How to find out the terminal command of an application? I'm trying to write a page that calls PHP that's stored in a MySQL database. The page that is stored in the MySQL database contains PHP (and HTML) code which I want to run on page load.  How could I go about doing this? Intraclass correlation coefficient vs. F-test (one-way ANOVA)? Here is a quesion that has been bugging me. You know how you can set up a program to run on start up right?  Well, how can i tell it to start minimized, like you would do in windows?   TeamViewer starts open, but I need it minimized, and Tray-it just doesn't seem as elegant of a way to do it. Methods to compute $\sum_{k=1}^nk^p$ without Faulhaber's formula Looking for Space Exploration Book which is collection of different sub-stories The Book is about an earthship exploring the frontiers that I read as a kid (80's). All the stories are told by the same character but different encounters and events.    The first story is about the crew exploring the planet in a number of separate ships when one of them is taken over by a telepathic alien. As the ship returns however the surprise is sprung on the alien that the Mother ship is Computer controlled. Another is the ship shrinking and then finding after its shrunk beyond the atom that it comes back in as gigantic compared to the Universe in a closed loop.   Hopefully that's enough to trigger a memory. Looking for a function $f$ that is $n$-differentiable, but $f^{(n)}$ is not continuous I am looking for a real valued function of real variable that is $n$-differentiable, but whose $n$th derivative is not continuous.  This is my function:  $f_n(x) = x^{n+1} \cdot \sin{\frac{1}{x}}$, if $x \neq 0$ and 0, if $x=0$. $n\in \{0,1,2,\ldots\}$ For example, if $n=0$, $f_0$ it's continuous and non diferentiable in $0$. Excluding empty(Null) fields when using EntityFieldQuery query condition A sine integral $\int_0^{\infty} \left(\frac{\sin x }{x }\right)^n\,\mathrm{d}x$ The following question comes from   post $$\int_0^{\infty} \left(\frac{\sin x }{x }\right)^n\,\mathrm{d}x$$ but now I'd be curious to know how to deal with it by methods of complex analysis. Some suggestions, hints? Thanks!!!  Sis. Session lost when switching from HTTP to HTTPS in PHP When sending the user to a checkout page, they are switched from http://sitename.com to https://sitename.com.  As a result, $_SESSION variables are lost.  The site has a valid SSL certificate which may or may not be of some use. From 8th June there's been a  period for people entering the UK. From 10th July, travellers from  via a travel-corridor. If more countries are added to the list of exemptions then the date for each country will be later than the 10th July. If a traveller entered the UK from a country that is not on the exempt list and that country is added to the exempt list prior to the end of the 14 day self-isolation period does the self-isolation period end early? For example, if an individual entered the UK from France on the 5th July, should they remain in isolation for 14 days or does the isolation end on the 10th when France is added to the list of exempt countries? My friend had a  on the board with 10 counters on it.  If I destroy it and he sacs it in response to gain the life, is there anything that can be done about that?  Like, can I put another destroy artifact on the stack to resolve before his sac resolves? Deleting self-answered question gets "Delete this answered question" warning How can I backup my RAW photos while travelling without Internet access? Direct product of Sylow subgroups Proposition II.7.5 of Hungerford's Algebra goes     Proposition 7.5. A finite group is nilpotent if and only if it is the direct product of its Sylow subgroups.   Let $G = (\mathbb{Z}_6, +)$. $G$ is abelian, so it is nilpotent and thus by the proposition is the direct product of its Sylow subgroups.  The Sylow subgroups of $G$ are $H = (\mathbb{Z}_3, +)$ and $K = (\mathbb{Z}_2, +)$, so the proposition says $G = H \times K$. In particular, $0 \in H$ and $0 \in K$, so $(0,0) \in H \times K = G$... but that's not true, since $G = \{0,1,2,3,4,5\}$.  Where is/are my misunderstanding(s)? Why does English spelling use silent letters? Why have a letter in a word when it’s silent in pronunciation, like the b in debt?  Can anyone please clarify my uncertainty here? Can't login with Google using Internet Accounts What is this “(arg: 1)” appearing in terminal/tty after I pressed a combination of keys? In a virtual terminal I (accidentally) pressed Win+1 and got a new line consisting of  (arg: 1)   As long as I keep Win pressed, all numbers I press get appended to the number in the parentheses, e.g. Win and 1+4 yields (arg:14).  When I (after releasing Win) press any key, that character is printed as many times as the number reads and the (arg: ...) expression is replaced by the standard prompt.  Does anybody know what happens here? Why would one limit username format? Many websites when ask you to create a new "username", will limit you in some way, most often like "username should only contain letters and underscores" or something alike.  This results in inability to use my email as username and forces me to come up with new unique string all the time.  What is the rationale behind this?  Why not just let users use their emails, as a simple to remember and always unique?   What is generally wrong with having symbols like *&amp;%$#@ as a part of username?