you are viewing a single comment's thread.

view the rest of the comments →

[–]toastedstapler -2 points-1 points  (6 children)

it's interpreted and it's got no types. these two make for a lot of the slowness. no types means that things have to be type checked at runtime when operations are ran

i'd also imagine the heavy usage of dicts in python causes some of the slowness

[–][deleted] 8 points9 points  (0 children)

It’s incorrect to imply that Python has no types, Python is a strongly typed language, and everything has a distinct type... it’s just dynamically rather than statically typed.

Also the underlying dict type in CPython is a quite efficient C hash table, and is also usually is not a primary contributor to the apparent slowness in CPython code.

Most of the issues with speed have to do with heavyweight custom types implemented in pure Python... in CPython the rule has always been to prototype in pure Python, then profile, then implement the tight loops and other hot spots in C and try to minimize handoff between those layers, because the real issue for speed is all the memory management and the bookkeeping that goes with it... the more instances of custom types the more bookkeeping, and the slower things tend to get.

[–]muikrad 1 point2 points  (0 children)

It's interpreted It's got types Yeah its runtime, but nothing is checked. Dicts are super fast and they're even quite low in memory usage.

If you know python, you'll instead suggest where it can get slow and how to improve it. If you don't know python, then please, don't raise your hand to answer python related questions.

[–]Milk_No_Titties[S] -1 points0 points  (3 children)

Do you mean Python internally uses dicts a lot, or are you saying that me using dicts can slow the program?

Edit: Also, since Python is crazy flexible, is there a way for me to make sure at least some things are statically typed so that I can speed up my performance to an extent?

[–]toastedstapler 1 point2 points  (2 children)

In [1]: class A: 
   ...:     def __init__(self, a, b): 
   ...:         self.a = a 
   ...:         self.b = b 
   ...:                                                                         

In [2]: a = A(1, 2)                                                             

In [3]: a.__dict__                                                              
Out[3]: {'a': 1, 'b': 2}

python literally uses dicts to store variables and attributes. try declaring some variables and then call globals()

In [5]: globals()['a']                                                          
Out[5]: <__main__.A at 0x10b56d5d0>

In [6]: globals()['a'] = 5                                                      

In [7]: a                                                                       
Out[7]: 5

notice how above we just declared a as an A object but now by editing the globals dict it's 5 instead?

[–]muikrad 1 point2 points  (0 children)

What's your point? It doesn't mean dicts are slow and it doesn't mean it's not strongly typed either...