A couple weeks ago I was reading a thread about character counting and Vegaseat made the comment that

Function calls are time expensive in Python, so I modified Sneekula's count_char2() approach by replacing all those calls to islower(), isupper(), isdigit() and isspace(), and also changing the order of if/elif to use the most common character test first.

I was intrigued, but I wanted to start a new thread because it seems like a new topic of discussion.

I was wondering if someone could expand a bit more on that statement. Why are function calls expensive in python, and is it more so than in other languages?

Instead of PMing you, why not post here. Unless for private things (which PM stands for), you should be displaying things here.

No critic/hurt inteded, but just correction, if you don't mind

Member Avatar for leegeorg07

yea im sorry im just so interested

dont worry I don't judge you because you are a year 8!
heeeeh

@leegeorg07
That article was fascinating! I still don't understand why function calls are time expensive...and I don't understand why explicit loops are more expensive than implied loops, but the article was still great!

But what's the big deal?

Personally i understand the big deal. Its about WHY they are expensive. What is it about calling a function that makes it harder on the program and if you want to make a super amazingly quick program and it had to be in python then maybe this could even matter (i doubt it) but still it is an interesting topic i think.

Member Avatar for leegeorg07

exactly i think that speed in programs come with understanding but thats not even why i'm interested, its because i never knew about that and if i can find some way of explaining it then i will just be amazed because i was always told that i was a complete and utter retard and this and other things would prove them wrong

Member Avatar for leegeorg07

im starting to think that function calls are only time expensive if you use them in a loop for example:

def ahasfh():
	start = time.time()
	time.sleep(5)
	total = time.time() - start
	print(total)

outputs:
5.0

however

for i in range(6):
	start2 = time.time()
	ahasfh()
	total2 = time.time() - start2
	print()
	print(total2)

outputs:

5.0

5.01600003242
5.0

5.06200003624
5.0

5.06299996376
5.0

5.04699993134
5.0

5.06200003624
5.0

5.06299996376

if you want to make a super amazingly quick program and it had to be in python then maybe this could even matter (i doubt it)

I doubt it as well. I view the "function calls are expensive" as someone trying to impress with how much they know. Function calls are expensive, AFAIK, because the function is in a different block of memory, so the program has to go to that block, execute it, and then come back to where it was. I care a __lot__ more about readability and being able to understand something that was written long before my little gray cells' memory of it. A function separates things nicely. If it takes an extra milli-second or two, fine.

Member Avatar for leegeorg07

i agree but i just find the idea of how and why function calls are expensive, i prefer a mix of readability and speed.

I agree with woooee. So what is function calls are "expensive"? If you want to make a program that's any sort of useful, you'd still have to use them.

I didn't read the article because I have far more interesting things to waste my time with, but I'd be interested in the titles of some of the other articles by its author.

Also an afterthought: If i wanted to make a "super amazingly quick program" I wouldn't use python anyway.

Another also I embarrassingly forgot: Function calls use a call stack, which is probably the reason why the guy said the were expensive (but I can't be sure).

Member Avatar for leegeorg07

and you wonder why your reputation is only 35 we are just wondering about it, we don't care about making fast programs all we're interested in is why and how it happens.

commented: I don't "wonder" why my rep is low. -1

You can snoop around Python code with this ...

import dis

def myfunc():
    a = 2
    b = 3
    print("adding a and b and 3")
    c = a + b + 3
    if c > 7:
        return c
    else:
        return None

# this disassembles the above function's code
dis.dis(myfunc)

"""my result with Python25 -->
 11           0 LOAD_CONST               1 (2)
              3 STORE_FAST               0 (a)

 12           6 LOAD_CONST               2 (3)
              9 STORE_FAST               1 (b)

 13          12 LOAD_CONST               3 ('adding a and b and 3')
             15 PRINT_ITEM          
             16 PRINT_NEWLINE       

 14          17 LOAD_FAST                0 (a)
             20 LOAD_FAST                1 (b)
             23 BINARY_ADD          
             24 LOAD_CONST               2 (3)
             27 BINARY_ADD          
             28 STORE_FAST               2 (c)

 15          31 LOAD_FAST                2 (c)
             34 LOAD_CONST               4 (7)
             37 COMPARE_OP               4 (>)
             40 JUMP_IF_FALSE            8 (to 51)
             43 POP_TOP             

 16          44 LOAD_FAST                2 (c)
             47 RETURN_VALUE        
             48 JUMP_FORWARD             5 (to 56)
        >>   51 POP_TOP             

 18          52 LOAD_CONST               0 (None)
             55 RETURN_VALUE        
        >>   56 LOAD_CONST               0 (None)
             59 RETURN_VALUE 
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.