Python concurrent.futures performance difference with little change

jhonatan teixeira

i got the following code:

from concurrent.futures import ThreadPoolExecutor, wait
import datetime

def calculate_mass(mass):
    for _ in range(10000):
        gravity = 9.8
        weight = mass * gravity


def perform_calculations():
    with ThreadPoolExecutor(3) as pool:
        pool.map(calculate_mass, range(1000))

if __name__ == '__main__':
    start = datetime.datetime.now()
    perform_calculations()
    end = datetime.datetime.now()

    print((end - start).total_seconds())

it takes 1.6467 seconds to execute on a core i7 3th generation with 8gb ram (dell notebook)

if change the this line

weight = mass * gravity

to

return mass * gravity

all of a sudden it takes 0.059083 seconds to complete.

Does anyone knows why that happens?

edit: I guess i'm getting tired. i didnt notice the loop was ending. my bad guys

Logan

With the line:

for _ in range(10000):
    gravity = 9.8
    weight = mass * gravity

It is iterating through the for loop 10,000 times. Which will take time.

However when you change it to:

for _ in range(10000):
    gravity = 9.8
    return mass * gravity

It is instead returning the value of mass * gravity after only 1 iteration through the for loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to monitor python's concurrent.futures.ProcessPoolExecutor?

python concurrent.futures.ProcessPoolExecutor: Performance of .submit() vs .map()

What is the difference between concurrent.futures and asyncio.futures?

Python 2.7 concurrent.futures.ThreadPoolExecutor does not parallelize

Parallelize a simple loop in Python and get results with concurrent.futures

In Python, is there an async equivalent to multiprocessing or concurrent.futures?

Concurrent.futures vs Multiprocessing in Python 3

Python: Wait on all of `concurrent.futures.ThreadPoolExecutor`'s futures

What's the difference between python's multiprocessing and concurrent.futures?

Will I get performance boost combining concurrent.futures with Flask

concurrent.futures.ThreadPoolExecutor swallowing exceptions (Python 3.6)

Python concurrent.futures using subprocess, running several python script

Multithreading with concurrent.futures in Python not working

How to use concurrent.futures in Python

using python concurrent.futures submit with processes numbering

Converting concurrent futures to Asyncio python3.7

Best way to get response from future in Python concurrent futures pool?

Python concurrent.futures - method not called

Finding the cause of a BrokenProcessPool in python's concurrent.futures

Python async loop concurrent.futures.ThreadPoolExecutor

Python Z3 and concurrent.futures

Using mutexes with concurrent futures in Python

Python Concurrent Futures for Panda Dataframe

Python concurrent.futures: threads don't start

Issue with python requests and concurrent.futures

Python: How to implement concurrent futures to a function

Python concurrent.futures

How to benchmark python function with concurrent.futures?

Python concurrency with concurrent.futures.ThreadPoolExecutor