Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:22:58+00:00 2026-06-04T07:22:58+00:00

I’m trying to benchmark a few methods against one another and for some reason

  • 0

I’m trying to benchmark a few methods against one another and for some reason I’m getting impossible results. Apparently, some of the operations are taking much longer than the whole application ran. I’ve searched it all out and for some reason I can’t pin down what I’m doing wrong, so I’m sorry, but I’m posting the whole method :-/

public static void parseNumber() throws Exception {
  long numberHelperTotal = 0;
  long numberUtilsTotal = 0;
  long regExTotal = 0;
  long bruteForceTotal = 0;
  long scannerTotal = 0;
  int iterations = 10;
  for (int i = 0; i < iterations; i++) {
    long numberHelper = 0;
    long numberUtils = 0;
    long regEx = 0;
    long bruteForce = 0;
    long scanner = 0;
    for (int j = 0; j < 999999; j++) { // I know 999999 is a bit overkill... I've tried it with smaller values and it still yields impossible results
      Date start; //I think it may have something to do with the way I'm doing start and end...
      Date end;
      Random rand = new Random();
      String string = ((rand.nextBoolean()) ? "" : "-") + String.valueOf(rand.nextDouble() * j);

      //NumberHelper
      start = new Date();
      NumberHelper.isValidNumber(double.class, string);
      end = new Date();
      numberHelper += end.getTime() - start.getTime();

      //NumberUtils
      start = new Date();
      NumberUtils.isNumber(string);
      end = new Date();
      numberUtils += end.getTime() - start.getTime();

      //RegEx
      start = new Date();
      Pattern p = Pattern.compile("^[-+]?[0-9]*\\.?[0-9]+$");
      Matcher m = p.matcher(string);
      if (m.matches()) {
        Double.parseDouble(string);
      }
      end = new Date();
      regEx += end.getTime() - start.getTime();

      //Brute Force (not international support) and messy support for E and negatives
      //This is not the way to do it...
      start = new Date();
      int decimalpoints = 0;
      for (char c : string.toCharArray()) {
        if (Character.isDigit(c)) {
          continue;
        }
        if (c != '.') {
          if (c == '-' || c == 'E') {
            decimalpoints--;
          } else {
            //return false
            //because it should never return false in this test, I will throw an exception here if it does.
            throw new Exception("Brute Force returned false! It doesn't work! The character is " + c + " Here's the number: " + string);
          }
        }
        if (decimalpoints > 0) {
          //return false
          //because it should never return false in this test, I will throw an exception here if it does.
          throw new Exception("Brute Force returned false! It doesn't work! The character is " + c + " Here's the number: " + string);
        }
        decimalpoints++;
      }
      end = new Date();
      bruteForce += end.getTime() - start.getTime();

      //Scanner
      start = new Date();
      Scanner scanNumber = new Scanner(string);
      if (scanNumber.hasNextDouble()) {//check if the next chars are integer
        //return true;
      } else {
        //return false;
        //because it should never return false in this test, I will throw an exception here if it does.
        throw new Exception("Scanner returned false! It doesn't work! Here's the number: " + string);
      }
      end = new Date();
      scanner += end.getTime() - start.getTime();

      //Increase averages
      numberHelperTotal += numberHelper;
      numberUtilsTotal += numberUtils;
      regExTotal += regEx;
      bruteForceTotal += bruteForce;
      scannerTotal += scanner;
      //For debug:
      //System.out.println("String: " + string);
      //System.out.println("NumberHelper: " + numberHelper);
      //System.out.println("NumberUtils: " + numberUtils);
      //System.out.println("RegEx: " + regEx);
      //System.out.println("Brute Force: " + bruteForce);
      //System.out.println("Scanner: " + scanner);
    }
  }

Sample output:

First: NumberUtils - 83748758 milliseconds
Second: Brute Force - 123797252 milliseconds
Third: NumberHelper - 667504094 milliseconds
Fourth: RegEx - 2540545193 milliseconds
Fifth: Scanner - 23447108911 milliseconds
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:22:59+00:00Added an answer on June 4, 2026 at 7:22 am

    Following 5 lines seem to be in incorrect place:

    //Increase averages
    numberHelperTotal += numberHelper;
    numberUtilsTotal += numberUtils;
    regExTotal += regEx;
    bruteForceTotal += bruteForce;
    scannerTotal += scanner;
    

    Try putting them outside of the for (int j ... loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.