2022-09-15 00:30:44 +02:00
|
|
|
import sys
|
|
|
|
import timeit
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from cpl_query.enumerable import Enumerable
|
|
|
|
from cpl_query.iterable import Iterable
|
|
|
|
|
2022-09-15 19:16:52 +02:00
|
|
|
VALUES = 10000
|
|
|
|
COUNT = 50
|
2022-09-15 00:30:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PerformanceTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
i = 0
|
|
|
|
self.values = []
|
|
|
|
while i < VALUES:
|
|
|
|
self.values.append(i)
|
|
|
|
i += 1
|
|
|
|
|
2022-09-15 12:48:05 +02:00
|
|
|
def test_range(self):
|
2022-12-01 23:41:06 +01:00
|
|
|
default = timeit.timeit(lambda: list(range(0, VALUES)), number=COUNT)
|
|
|
|
iterable = timeit.timeit(lambda: Iterable.range(0, VALUES), number=COUNT)
|
|
|
|
enumerable = timeit.timeit(lambda: Enumerable.range(0, VALUES), number=COUNT)
|
2022-09-15 12:48:05 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
print("Range")
|
|
|
|
print(f"d: {default}s")
|
|
|
|
print(f"i: {iterable}s")
|
|
|
|
print(f"e: {enumerable}s")
|
2022-09-15 12:48:05 +02:00
|
|
|
|
2022-12-20 14:34:55 +01:00
|
|
|
self.assertAlmostEqual(round(default, 3), round(enumerable, 3))
|
|
|
|
self.assertAlmostEqual(round(default, 3), round(iterable, 3))
|
2022-09-15 00:30:44 +02:00
|
|
|
|
|
|
|
def test_where_single(self):
|
2022-12-01 23:41:06 +01:00
|
|
|
default = timeit.timeit(lambda: [x for x in list(range(0, VALUES)) if x == 50], number=COUNT)
|
|
|
|
iterable = timeit.timeit(lambda: Iterable.range(0, VALUES).where(lambda x: x == 50).single(), number=COUNT)
|
|
|
|
enumerable = timeit.timeit(lambda: Enumerable.range(0, VALUES).where(lambda x: x == 50).single(), number=COUNT)
|
2022-09-15 12:48:05 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
print("Where single")
|
|
|
|
print(f"d: {default}s")
|
|
|
|
print(f"i: {iterable}s")
|
|
|
|
print(f"e: {enumerable}s")
|
2022-09-15 18:14:40 +02:00
|
|
|
|
|
|
|
self.assertLess(default, enumerable)
|
|
|
|
self.assertLess(default, iterable)
|
|
|
|
|
|
|
|
def test_where_single_complex(self):
|
|
|
|
class TestModel:
|
|
|
|
def __init__(self, v, tm=None):
|
|
|
|
self.value = v
|
|
|
|
self.tm = tm
|
|
|
|
|
|
|
|
values = []
|
|
|
|
for i in range(VALUES):
|
|
|
|
values.append(TestModel(i, TestModel(i + 1)))
|
|
|
|
|
2022-12-01 23:41:06 +01:00
|
|
|
default = timeit.timeit(lambda: [x for x in values if x.tm.value == 50], number=COUNT)
|
2023-02-20 15:55:20 +01:00
|
|
|
iterable = timeit.timeit(
|
|
|
|
lambda: Iterable(TestModel, values).where(lambda x: x.tm.value == 50).single(), number=COUNT
|
|
|
|
)
|
|
|
|
enumerable = timeit.timeit(
|
|
|
|
lambda: Enumerable(TestModel, values).where(lambda x: x.tm.value == 50).single(), number=COUNT
|
|
|
|
)
|
|
|
|
|
|
|
|
print("Complex where single")
|
|
|
|
print(f"d: {default}s")
|
|
|
|
print(f"i: {iterable}s")
|
|
|
|
print(f"e: {enumerable}s")
|
2022-09-15 12:48:05 +02:00
|
|
|
|
|
|
|
self.assertLess(default, enumerable)
|
|
|
|
self.assertLess(default, iterable)
|