50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
from cpl.core.console import Console
|
|
from cpl.core.utils.benchmark import Benchmark
|
|
from cpl.query.collection import Collection
|
|
from cpl.query.enumerable import Enumerable
|
|
from cpl.query.list import List
|
|
from cpl.query.immutable_list import ImmutableList
|
|
from cpl.query.set import Set
|
|
|
|
|
|
def _default():
|
|
Console.write_line(Enumerable.empty().to_list())
|
|
|
|
Console.write_line(Enumerable.range(0, 100).length)
|
|
Console.write_line(Enumerable.range(0, 100).to_list())
|
|
|
|
Console.write_line(Enumerable.range(0, 100).where(lambda x: x % 2 == 0).length)
|
|
Console.write_line(
|
|
Enumerable.range(0, 100).where(lambda x: x % 2 == 0).to_list().select(lambda x: str(x)).to_list()
|
|
)
|
|
Console.write_line(List)
|
|
|
|
s =Enumerable.range(0, 10).to_set()
|
|
Console.write_line(s)
|
|
s.add(1)
|
|
Console.write_line(s)
|
|
|
|
|
|
def t_benchmark(data: list):
|
|
Benchmark.all("Enumerable", lambda: Enumerable(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
|
Benchmark.all("Set", lambda: Set(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
|
Benchmark.all("Collection", lambda: Collection(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
|
Benchmark.all("List", lambda: List(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
|
Benchmark.all(
|
|
"ImmutableList", lambda: ImmutableList(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list()
|
|
)
|
|
Benchmark.all("List comprehension", lambda: [x * 2 for x in data if x % 2 == 0])
|
|
|
|
|
|
def main():
|
|
N = 10_000_000
|
|
data = list(range(N))
|
|
t_benchmark(data)
|
|
|
|
Console.write_line()
|
|
_default()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|