Advertisement
avp210159

Java 8 filter().count vs Character.isDigit

Dec 14th, 2014
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. // http://www.compilejava.net/
  2.  
  3. //*******************************************************************
  4. // Java compiler created in PHP to quickly and safely test code.
  5. // NOTE: please read the 'More Info' tab to the right for shortcuts.
  6. //*******************************************************************
  7.  
  8. import java.lang.Math; // header stuff MUST go above the first class
  9.  
  10. // our main class becomes a file but the main method is still found
  11. public class HelloWorld
  12. {
  13.   public static void main(String[] args)
  14.   {
  15.     String s = "";
  16.     int i = 0, j = 0;
  17.    
  18.     for (i = 0; i < 10; i++)
  19.         s += "aqw1ert2x4";
  20.     long start_time = System.currentTimeMillis();
  21.     long count = 0;
  22.         for (j = 0; j < 1000000; j++) {
  23.       /* * /
  24.         for (i = 0; i < s.length(); i++) {
  25.             if (Character.isDigit(s.charAt(i)))
  26.                 count++;  // 259 ms
  27.         }
  28.       /*  */
  29.              long cnt = s.codePoints().filter(Character::isDigit).count();
  30.              count += cnt;  // 1405 ms
  31.       /* */
  32.          }
  33.    
  34.     long stop_time = System.currentTimeMillis();
  35.     System.out.println("count " + count + " " + (stop_time - start_time));
  36.  
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement