diff --git a/tests/timeit/31.class.dynamic.attributes.py b/tests/timeit/31.class.dynamic.attributes.py new file mode 100755 index 00000000..35afe370 --- /dev/null +++ b/tests/timeit/31.class.dynamic.attributes.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2025 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from dataclasses import dataclass +import timeit + +@dataclass +class Data(): + a:int + b:str + +class A(): + p1 = Data(1,'a') + p2 = Data(2,'b') + p3 = Data(3,'c') + p4 = Data(4,'d') + p5 = Data(5,'e') + +class B(): + p1:Data + p2:Data + p3:Data + p4:Data + p5:Data + +B.p1 = Data(1,'a') +B.p2 = Data(2,'b') +B.p3 = Data(3,'c') +B.p4 = Data(4,'d') +B.p5 = Data(5,'e') + +def test_ti_01(): + return len([A.p1.b, A.p1.a, A.p2.b, A.p2.a, A.p3.b, A.p3.a, A.p4.b, A.p4.a, A.p5.b, A.p5.a]) +def test_ti_02(): + return len([B.p1.b, B.p1.a, B.p2.b, B.p2.a, B.p3.b, B.p3.a, B.p4.b, B.p4.a, B.p5.b, B.p5.a]) +def test_ti_03(): + return len([A.p1.b, A.p1.a, A.p2.b, A.p2.a, A.p3.b, A.p3.a, A.p4.b, A.p4.a, A.p5.b, A.p5.a]) +def test_ti_04(): + return len([B.p1.b, B.p1.a, B.p2.b, B.p2.a, B.p3.b, B.p3.a, B.p4.b, B.p4.a, B.p5.b, B.p5.a]) +def test_ti_05(): + return len([A.p1.b, A.p1.a, A.p2.b, A.p2.a, A.p3.b, A.p3.a, A.p4.b, A.p4.a, A.p5.b, A.p5.a]) +def test_ti_06(): + return len([B.p1.b, B.p1.a, B.p2.b, B.p2.a, B.p3.b, B.p3.a, B.p4.b, B.p4.a, B.p5.b, B.p5.a]) + +loop = 10000 + +a:dict = {} + +for testName in sorted([tn for tn in globals() if tn.startswith('test_ti_')]): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"{testName} | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") diff --git a/tests/timeit/32.enum.vs.flag.vs.bitmap.py b/tests/timeit/32.enum.vs.flag.vs.bitmap.py new file mode 100755 index 00000000..34e17508 --- /dev/null +++ b/tests/timeit/32.enum.vs.flag.vs.bitmap.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2025 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import annotations + +from dataclasses import dataclass +from enum import Enum,Flag,auto +import timeit + +class PermBm(int): + READ = 0x01 + WRITE = 0x02 + EXECUTE = 0x04 + +class PermEn(Enum): + READ = 0x01 + WRITE = 0x02 + EXECUTE = 0x04 + +class PermFl(Flag): + READ = auto() + WRITE = auto() + EXECUTE = auto() + +class PermCFl(): + READ:PermCFl + WRITE:PermCFl + EXECUTE:PermCFl + __slots__= ('_v') + _v:int + def __init__(self,v:int): + self._v = v + def __or__(self, other:PermCFl): + return PermCFl(self._v|other._v) + def __and__(self, other:PermCFl): + return PermCFl(self._v&other._v) + def __str__(self): + return f"PermCFL({self._v})" +PermCFl.READ = PermCFl(0x01) +PermCFl.WRITE = PermCFl(0x02) +PermCFl.EXECUTE = PermCFl(0x04) + +fff = PermFl.READ | PermFl.WRITE + +print(fff) + +def test_ti_0_01(): + f1 = PermFl.READ | PermFl.WRITE + f2 = PermFl.READ | PermFl.EXECUTE + f3 = PermFl.WRITE | PermFl.EXECUTE + return f1 | f2 | f3 +def test_ti_0_02(): + f1 = PermCFl.READ | PermCFl.WRITE + f2 = PermCFl.READ | PermCFl.EXECUTE + f3 = PermCFl.WRITE | PermCFl.EXECUTE + return f1 | f2 | f3 +def test_ti_0_03(): + f1 = PermBm.READ | PermBm.WRITE + f2 = PermBm.READ | PermBm.EXECUTE + f3 = PermBm.WRITE | PermBm.EXECUTE + return f1 | f2 | f3 +def test_ti_1_01(): + f1 = PermFl.READ | PermFl.WRITE + f2 = PermFl.READ | PermFl.EXECUTE + f3 = PermFl.WRITE | PermFl.EXECUTE + return 123 +def test_ti_1_02(): + f1 = PermCFl.READ | PermCFl.WRITE + f2 = PermCFl.READ | PermCFl.EXECUTE + f3 = PermCFl.WRITE | PermCFl.EXECUTE + return 123 +def test_ti_1_03(): + f1 = PermBm.READ | PermBm.WRITE + f2 = PermBm.READ | PermBm.EXECUTE + f3 = PermBm.WRITE | PermBm.EXECUTE + return 123 +def test_ti_2_03(): + f1 = PermEn.READ + f2 = PermEn.READ + f3 = PermEn.WRITE + return f1 , f2 , f3 +def test_ti_2_04(): + f1 = PermFl.READ + f2 = PermFl.READ + f3 = PermFl.WRITE + return f1 , f2 , f3 +def test_ti_2_05(): + f1 = PermCFl.READ + f2 = PermCFl.READ + f3 = PermCFl.WRITE + return f1 , f2 , f3 +def test_ti_2_06(): + f1 = PermBm.READ + f2 = PermBm.READ + f3 = PermBm.WRITE + return f1 , f2 , f3 + + +loop = 100000 + +a:dict = {} + +for testName in sorted([tn for tn in globals() if tn.startswith('test_ti_')]): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"{testName} | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}")