Skip to content
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

Improve performance of fake_username #44

Merged
merged 4 commits into from
Apr 23, 2020
Merged

Improve performance of fake_username #44

merged 4 commits into from
Apr 23, 2020

Conversation

caioariede
Copy link
Contributor

Description

This PR introduces a backwards-incompatible change by removing the rand_range argument from fake_username. With our focus being on performance, getting rid of expensive calls, such as random, is essential. The same/old behavior can still be accomplished using a custom function.

The impact of this change, being backwards-incompatible, is still minimal since as mentioned above the same behavior can still be accomplished using a custom function, and, the rand_range argument was optional.

Profiling results

Before changes

Total time: 35.0136 s
File: anon/utils.py
Function: fake_username at line 260

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   260                                           @profile
   261                                           def fake_username(max_size=10, separator="", rand_range=(1000, 999999)):
   262                                               """ Returns fake username
   263
   264                                               :max_size: Maximum number of chars
   265                                               :separator: Word separator
   266                                               :rand_range: Range to use when generating random number
   267
   268                                               """
   269   1000000    1481137.0      1.5      4.2      rand_start, rand_end = rand_range
   270   1000000    1197990.0      1.2      3.4      if not rand_end > rand_start:
   271                                                   raise ValueError(
   272                                                       "rand_range start ({}) must be > end ({})".format(rand_start, rand_end)
   273                                                   )
   274
   275   1000000   18277722.0     18.3     52.2      random_number = str(random.randint(rand_start, rand_end))
   276   1000000    1983915.0      2.0      5.7      min_size_allowed = _min_word_size + len(random_number)
   277
   278   1000000    1242765.0      1.2      3.5      if max_size < min_size_allowed:
   279                                                   raise ValueError("username must be >= {}".format(min_size_allowed))
   280                                               else:
   281   1000000    1369452.0      1.4      3.9          max_size -= len(random_number)
   282
   283   1000000    9460604.0      9.5     27.0      return fake_text(max_size, separator=separator) + random_number

After changes

Total time: 15.1933 s (2.3 times faster)
File: anon/utils.py
Function: fake_username at line 290

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   290                                           @profile
   291                                           def fake_username(max_size=10, separator=""):
   292                                               """ Returns fake username
   293
   294                                               :max_size: Maximum number of chars
   295                                               :separator: Word separator
   296                                               :rand_range: Range to use when generating random number
   297
   298                                               """
   299   1000000    2576025.0      2.6     17.0      random_number = str(next(_small_int_generator))
   300   1000000    2003946.0      2.0     13.2      min_size_allowed = _min_word_size + len(random_number)
   301
   302   1000000    1035719.0      1.0      6.8      if max_size < min_size_allowed:
   303                                                   raise ValueError("username must be >= {}".format(min_size_allowed))
   304                                               else:
   305   1000000    1324213.0      1.3      8.7          max_size -= len(random_number)
   306
   307   1000000    8253427.0      8.3     54.3      return fake_text(max_size, separator=separator) + random_number

Todos

  • Tests
  • Documentation
  • Changelog

@github-actions
Copy link

github-actions bot commented Apr 23, 2020

File Coverage
All files 95%
anon/init.py 63%
anon/base.py 97%
anon/utils.py 87%
tests/compat.py 50%

Minimum allowed coverage is 50%

Generated by 🐒 cobertura-action against d644cd2

@caioariede caioariede merged commit 5dcaf1c into master Apr 23, 2020
@caioariede caioariede deleted the fix/c83b8f branch April 23, 2020 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants