Home > Mobile >  traitlets python3 performance - Int() and Unicode()
traitlets python3 performance - Int() and Unicode()

Time:10-04

What is the performance hit for using traitlets.Int() and traitlets.Unicode() under python 3.9?

CodePudding user response:

The performance hit for traitlets.Int() and traitlets.Unicode() was about 25 to 35 times slower than Python 3.9.14 stdlib.

This is the outcome of one performance-test run.

  • Overall, I think the pure-python traitlets implementation adds a fairly high cost for what we get; however, there are obviously some situations that would benefit greatly from traitlets.
  • Consider enthought traits.api if performance is required; the performance-critical components of enthought traits.api are compiled.
$ python -V
Python 3.9.14
$
$ uname -a
Darwin WS-T604GF3GK0.tld 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 x86_64
$
$ python traitlets_test.py
After traitlets initialization, traitlets Unicode() was 24.0261 times slower than python stdlib
After traitlets initialization, traitlets Int() was 26.2789 times slower than python stdlib
$

If anyone wants to see what I used to test, this was the script...

# filename: traitlets_test.py

from timeit import timeit
import sys

from traitlets import HasTraits, Int, Unicode

###############################################
# Define traitlets tests here
###############################################

class MyTraitTester(HasTraits):
    """
    Initialize a HasTraits() class instance and define static
    types for foo_int and foo_unicode.
    """
    foo_int = Int()
    foo_unicode = Unicode()

def with_traits_int():
    harness.foo_int = 5
    return harness.foo_int

def with_traits_unicode():
    harness.foo_unicode = "5555555555555555555555555555555555555555555555555555"
    return harness.foo_unicode

###############################################
# Define python stdlib tests here...
###############################################

def without_traits_int():
    foo_int = 5
    return foo_int

def without_traits_unicode():
    foo_unicode = "5555555555555555555555555555555555555555555555555555"
    return foo_unicode

if __name__=="__main__":
    # Initialize traitlets here... `traitlets.HasTraits()` looks very slow... I will not test performance with it.
    harness = MyTraitTester()

    time_with_traits = timeit("with_traits_unicode()", setup="from __main__ import with_traits_unicode", number=1000000)
    time_without_traits = timeit("without_traits_unicode()", setup="from __main__ import without_traits_unicode", number=1000000)
    print("After traitlets initialization, traitlets Unicode() was {} times slower than python stdlib".format(round(time_with_traits/time_without_traits, 4)))

    time_with_traits = timeit("with_traits_int()", setup="from __main__ import with_traits_int", number=1000000)
    time_without_traits = timeit("without_traits_int()", setup="from __main__ import without_traits_int", number=1000000)
    print("After traitlets initialization, traitlets Int() was {} times slower than python stdlib".format(round(time_with_traits/time_without_traits, 4)))
  • Related