summaryrefslogtreecommitdiff
path: root/tests/profile_natsorted.py
blob: a2b1a5ce364273697d16074553931ce952c868ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""\
This file contains functions to profile natsorted with different
inputs and different settings.
"""

import cProfile
import locale
import sys

try:
    from natsort import ns, natsort_keygen
except ImportError:
    sys.path.insert(0, ".")
    from natsort import ns, natsort_keygen

locale.setlocale(locale.LC_ALL, "en_US.UTF-8")

# Samples to parse
number = 14695498
int_string = "43493"
float_string = "-434.93e7"
plain_string = "hello world"
fancy_string = "7abba9342fdab"
a_path = "/p/Folder (1)/file (1).tar.gz"
some_bytes = b"these are bytes"
a_list = ["hello", "goodbye", "74"]

basic_key = natsort_keygen()
real_key = natsort_keygen(alg=ns.REAL)
path_key = natsort_keygen(alg=ns.PATH)
locale_key = natsort_keygen(alg=ns.LOCALE)


def prof_time_to_generate():
    print("*** Generate Plain Key ***")
    for _ in range(100000):
        natsort_keygen()


cProfile.run("prof_time_to_generate()", sort="time")


def prof_parsing(a, msg, key=basic_key):
    print(msg)
    for _ in range(100000):
        key(a)


cProfile.run(
    'prof_parsing(int_string, "*** Basic Call, Int as String ***")', sort="time"
)
cProfile.run(
    'prof_parsing(float_string, "*** Basic Call, Float as String ***")', sort="time"
)
cProfile.run('prof_parsing(float_string, "*** Real Call ***", real_key)', sort="time")
cProfile.run('prof_parsing(number, "*** Basic Call, Number ***")', sort="time")
cProfile.run(
    'prof_parsing(fancy_string, "*** Basic Call, Mixed String ***")', sort="time"
)
cProfile.run('prof_parsing(some_bytes, "*** Basic Call, Byte String ***")', sort="time")
cProfile.run('prof_parsing(a_path, "*** Path Call ***", path_key)', sort="time")
cProfile.run('prof_parsing(a_list, "*** Basic Call, Recursive ***")', sort="time")
cProfile.run(
    'prof_parsing("434,930,000 dollars", "*** Locale Call ***", locale_key)',
    sort="time",
)