-1

I wrote simplePrint.py

simplePrint.py

print(1)
print(2)
print(3)

And, I wrote functionPrint.py.

functionPrint.py

def printTestFunction(one,two,three):
   print(one)
   print(two)
   print(three)
printTestFunction(1,2,3)

It may be natural, but functionPrint.py is slower.

Is there a way to speed up processing while using functions?

The speed comparison method is as follows

import timeit

class checkTime():
    def __init__(self):
        self.a = 0
        self.b = 0
        self.c = 0

    def local(self):
        print(1)
        print(2)
        print(3)

    def self(self):
        def printTestFunction(one,two,three):
            print(one)
            print(two)
            print(three)
        printTestFunction(1,2,3)

    def getTime(self):

        def test1():
            self.self()
        self_elapsed_time = timeit.Timer(stmt=test1).repeat(number=10000)

        def test2():
            self.local()
        local_elapsed_time = timeit.Timer(stmt=test2).repeat(number=10000)

        print ("local_time:{0}".format(local_elapsed_time) + "[sec]")
        print ("self_time:{0}".format(self_elapsed_time) + "[sec]")

checkTime = checkTime()
checkTime.getTime()

result

local_time:[0.04716750000000003, 0.09638709999999995, 0.07357000000000002, 0.04696279999999997, 0.04360750000000002][sec]
self_time:[0.09702539999999998, 0.111617, 0.07951390000000003, 0.08777400000000002, 0.099128][sec]
petezurich
  • 9,280
  • 9
  • 43
  • 57
taichi
  • 631
  • 1
  • 7
  • 26
  • 1
    How are you benchmarking? There will be some overhead, but I would expect it to be non-noticeable in most contexts. – Carcigenicate Jan 24 '20 at 19:13
  • Not really.. the processing cost of invoking a method vs inlining code is quite negligible. – gabriel.hayes Jan 24 '20 at 19:14
  • 2
    Even in lower-level languages like C, functions add overhead as the stack must be manipulated for the function. Especially in languages with more overhead such as Python, extra processing time is inevitable (though it shouldn't be at all noticeable at this level) – clubby789 Jan 24 '20 at 19:14

2 Answers2

1

There are plenty of ways to optimize your Python, but for something this simple, I wouldn't worry about it. Function calls are damn near instantaneous in human time.

A function call in most languages has to create new variables for the arguments, create a local scope, perform all the actions. So:

def printTestFunction(one,two,three):
   print(one)
   print(two)
   print(three)
printTestFunction(1,2,3)

runs something like this:

define function printTestFunction
call function with args (1, 2, 3)
create local scope
one=arg[0]
two=arg[1]
three=arg[2]
print(one)
print(two)
print(three)
return None
destroy local scope
garbage collect

That's my guess anyway. You can see that there's a lot more going on here and that takes time. (in particular, creating a local scope is a lot of instructions).

(You should definitely still use functions as programming anything complex gets out of control VERY quickly without them. The speed bump is negligible.)

Alex Weavers
  • 710
  • 3
  • 9
0

Doing a simple Google search yields:

Here are 5 important things to keep in mind in order to write efficient Python cde...

  1. Know the basic data structures. ...
  2. Reduce memory footprint. ...
  3. Use builtin functions and libraries. ...
  4. Move calculations outside the loop. ...
  5. Keep your code base small.

If you want to test your scripts and see which runs faster you can use this (taken from this post):
import time
start_time = time.time()

// Your main code here

print("--- %s seconds ---" % (time.time() - start_time))