#๐Ÿ”’ does type hinting improve performance?

17 messages ยท Page 1 of 1 (latest)

high tundra
#

I know, there's thousands of websites saying it doesn't... but my tests say it actually does and that pretty reliably
code:

from timeit import timeit


def hinting(v: int) -> int:
    a: int = 0
    for _ in range(v):
        a += 1
    return a



def no_hinting(v):
    a = 0
    for _ in range(v):
        a += 1
    return a


x = 10
print("Hinting:", timeit(lambda: hinting(x), number=1000000))
print("No hinting:", timeit(lambda: no_hinting(x), number=1000000))

the hinting() function is faster about 5 times times for every time the no_hinting() function is faster

and trust me, I ran that script very often because I had a little disagreement with chatgpt over it

still cosmosBOT
#

@high tundra

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

high tundra
nocturne slate
#

!timeit

def no_hinting(v):
    a = 0
    for _ in range(v):
        a += 1
    return a

no_hinting(10)
still cosmosBOT
nocturne slate
#

!timeit

def hinting(v: int) -> int:
    a: int = 0
    for _ in range(v):
        a += 1
    return a

hinting(10)
still cosmosBOT
umbral stone
#

type hints incur minor overhead

nocturne slate
#

Yeah, that's what I thought
It takes a fraction of a second for the compiler to throw them away

umbral stone
#

it doesn't throw them away

#

it's parsed, evaluated and stored

nocturne slate
#

oh right
now it keeps them and puts them in annotations

umbral stone
#

it's very impressive when you consider all that is being done in 100ns

#

i dont actually know if the compiler is doing any optimizations here

storm osprey
#

for sure improves developer performance

still cosmosBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.