Python algorithm call?

I have a function python(call it myFunction) that receives a list of numbers as input and, after a complicated calculation, returns the result of the calculation (which is a number ).

The function is as follows:

def myFunction( listNumbers ):
    # initialize the result of the calculation
    calcResult = 0

    # looping through all indices, from 0 to the last one
    for i in xrange(0, len(listNumbers), 1):
        # some complex calculation goes here, changing the value of 'calcResult'

    # let us now return the result of the calculation
    return calcResult

I tested the function and it works as expected.

An myFunctionargument is usually provided listNumberscontaining 5,000,000 elements. As expected, the calculation takes time. I need this function to execute as quickly as possible

: , 5 , listNumbers 4999,999 . , LAST VALUE . 6 .

, (1- ): 6am. listNumbers, myFunction. , , myFunction ( , ). , - 6 .

(2- ): ( 5 ) listNumbers 4999,999 , myFunction, , (, ), - 6 - "" - . , 6 , , , 6 .

, myFunction . / , myFunction - ( ), , 1-?

, c++/numpy + cython/parallel computing .., . - , - , .

+5
8

generator. , .

: , :)

class lazylist(object):
    def __init__(self):
        self.cnt = 0
        self.length = 5000000

    def __iter__(self):
        return self

    def __len__(self):
        return self.length

    def next(self):
        if self.cnt < self.length:
            self.cnt += 1
            #return data here or wait for it
            return self.cnt #just return a counter for this example
        else:
            raise StopIteration()

    def __getitem__(self, i):
        #again, block till you have data.
        return i+1 #simple counter

myFunction(lazylist())

. , len , , . for e in li enumerate - .

+10

" ", list?

  • , . Python , , , . yield, generator.

    def delayed_list():
        for val in numpy_array[:4999999]:
            yield val
        wait_until_6am()
        yield numpy_array[4999999]
    

    myFunction(delayed_list())
  • , :)

, PEP8 Python:

  • my_function myFunction
  • for i, val in enumerate(numbers): for i in xrange(0, len(listNumbers), 1): ..
+5

, , , .

import threading
import time

class lastblocks(list):
    def __init__(self,*args,**kwargs):
        list.__init__(self,*args,**kwargs)
        self.e = threading.Event()
    def __getitem__(self, index):
        v1 = list.__getitem__(self,index)
        if index == len(self)-1:
            self.e.wait()
            v2 = list.__getitem__(self,index)
            return v2
        else:
            return v1


l = lastblocks(range(5000000-1)+[None])

def reader(l):
    s = 0
    for i in xrange(len(l)):
        s += l[i]
    print s

def writer(l):
    time.sleep(10)
    l[5000000-1]=5000000-1
    l.e.set()
    print "written"

reader = threading.Thread(target=reader, args=(l,))
writer = threading.Thread(target=writer, args=(l,))
reader.start()
writer.start()

:

written
12499997500000

numpy:

import threading
import time

import numpy as np

class lastblocks(np.ndarray):
    def __new__(cls, arry):
        obj = np.asarray(arry).view(cls)
        obj.e = threading.Event()
        return obj
    def __array_finalize__(self, obj):
        if obj is None: return
        self.e = getattr(obj, 'e', None)

    def __getitem__(self, index):
        v1 = np.ndarray.__getitem__(self,index)
        if index == len(self)-1:
            self.e.wait()
            v2 = np.ndarray.__getitem__(self,index)
            return v2
        else:
            return v1


l = lastblocks(np.asarray(range(5000000-1)+[None]))

def reader(l):
    s = 0
    for i in xrange(len(l)):
        s += l[i]
    print s

def writer(l):
    time.sleep(10)
    l[5000000-1]=5000000-1
    l.e.set()
    print "written"

reader = threading.Thread(target=reader, args=(l,))
writer = threading.Thread(target=writer, args=(l,))
reader.start()
writer.start()
+4

, , ( ), .

- , , ( ). , , , .

, , 06:00, .

+1
+1

, myFunction. , , . . /, . , , myFunction , , pythonic :

def myFunction( listNumbers ):
    calcResult = 0

    # enumerate if you really need an index of element in array
    for n,v in enumerate(listNumbers):
        # some complex calculation goes here, changing the value of 'calcResult'

    return calcResult

. , , __iter__ ( ); , , , - .

, __getitem__, Dan D. , .

+1

:

def fnc(lst):
    result = 0
    index = 0
    while index < len(lst):
        while index < len(lst):
            ... do some manipulations here ...
            index += 1
        yield result

lst = [1, 2, 3]
gen = fnc(lst)
print gen.next()

lst.append(4)
print gen.next()
+1

- :

processedBefore6 = myFunction([1,2,3]) # the first 4,999,999 vals.

while lastVal.notavailable:
  sleep(1)

processedAfter6 = myFunction([processedBefore6, lastVal])

( 1 โ†’ 2 โ†’ 3 ..), , , .

0

All Articles