Skip to content

Speed up Number#numberBetween for int values #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2023

Conversation

snuyanzin
Copy link
Collaborator

@snuyanzin snuyanzin commented May 20, 2023

The test to check

    @Test
    void test() {
        for (int i = 0; i < 100_000_000; i++) {
            faker.number().numberBetween(-1000_000_000, 2000_000_000);
        }
    }

before the change it takes about 20x times longer to complete ...

the main idea is in case of overloading the limit use fallback to long instead of fallback to BigDecimal

@what-the-diff
Copy link

what-the-diff bot commented May 20, 2023

PR Summary

  • Updated method parameters
    The method 'numberBetween(int min, int max)' now uses the 'final' keyword for its parameters, ensuring they remain constant throughout the method.

  • Optimized variable calculation
    A new variable 'amplitude' of type long is declared and initialized to avoid duplicate calculations, improving efficiency.

  • Enhanced input validation
    The input validation for the method now checks both minimum and maximum bounds to ensure a valid range, allowing it to work with positive and negative numbers.

  • Improved return value handling
    The method returns an integer value casted from a double to better handle large ranges and potential overflow, without the need for throwing exceptions.

@snuyanzin snuyanzin merged commit 2bade94 into datafaker-net:main May 20, 2023
@bodiam
Copy link
Contributor

bodiam commented May 20, 2023

Do the final parameters make any difference here?

@snuyanzin
Copy link
Collaborator Author

right now no difference
the only reason I made it it is kind of self-protection from possible reassigning values in methods in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants