Python list comprehension expensive

You are never calling your squares function, so it is not doing anything. List comprehensions are in fact faster: >>> import timeit >>> def squares(values): … lst = [] … for x in range(values): … lst.append(x*x) … return lst … >>> def squares_comp(values): … return [x*x for x in range(values)] … >>> timeit.timeit(‘f(10)’, ‘from __main__ … Read more

How to parallelize list-comprehension calculations in Python?

As Ken said, it can’t, but with 2.6’s multiprocessing module, it’s pretty easy to parallelize computations. import multiprocessing try: cpus = multiprocessing.cpu_count() except NotImplementedError: cpus = 2 # arbitrary default def square(n): return n * n pool = multiprocessing.Pool(processes=cpus) print(pool.map(square, range(1000))) There are also examples in the documentation that show how to do this using … Read more