Exemple #1
0
    def fastmap(self, func, iterable):
        main_timer = timeit.Timer()

        iterable = list(iterable)  # TODO
        if not iterable:
            self.log.warning("Returning empty set due to empty iterable")
            return []

        self.log.info(f"Mapping {func.__name__} to {len(iterable)} items")

        # if isinstance(iterable, types.GeneratorType): # TODO
        #     first_batch = list(itertools.islice(iterable, num_threads))
        first_batch = iterable[:self.num_threads]
        iterable = iterable[self.num_threads:]
        avg_size_element = len(json.dumps(first_batch)) / self.num_threads

        mapped_results = []
        with Pool(self.num_threads) as local_pool:
            timed_func_results = local_pool.map(lambda e: _timed_func(func, e), first_batch)
        avg_local_dur = sum(r[0] for r in timed_func_results) / self.num_threads
        mapped_results += [r[1] for r in timed_func_results]

        sources = _get_full_sources(func)
        module_name = func.__module__.__name__
        func_name = func.__name__
        func_hash = _gen_hash(sources, module_name, func_name)

        if func_hash in _transfer_durs:
            avg_transfer_dur = _transfer_durs[func_hash]
        else:
            start_time = time.time()
            post_payload = json.dumps({"source": sources,
                                       "module_name": module_name,
                                       "func_name": func_name})
            resp = post_json(Defaults.CLOUD_URL + "/api/v1/init_map", post_payload)
            elapsed_time = time.time() - start_time
            transfer_time = elapsed_time - resp['elapsed_time']

        avg_transfer_dur = 0  # TODO
        if True:  # avg_local_dur < _config.avg_transfer_dur():
            self.log.info(f"Local-execution-time < item-transfer-time ({avg_local_dur}s < {avg_transfer_dur}s). Executing entire map locally"),
            with Pool(self.num_threads) as local_pool:
                mapped_results += local_pool.map(func, iterable)
            self.log.info(f"Processed {len(iterable)} items in {main_timer.timeit()}s")
            return mapped_results

        self.log.info(f"item-transfer-time < local-execution-time ({avg_local_dur}s < {avg_transfer_dur}s). Executing on cloud and locally"),
Exemple #2
0
 def iSort(self, r1):
     '''
     Function for insertion sort
     - always maintains a sorted sublist in 
     the lower positions of the list. 
     - Each new item is then “inserted” back 
     into the previous sublist such that the 
     sorted sublist is one item larger.
     '''
     print("iSort")
     for index in range(1, len(r1)):
         currentvalue = r1[index]
         position = index
         while position > 0 and r1[position - 1] > currentvalue:
             r1[position] = r1[position - 1]
             position = position - 1
         r1[position] = currentvalue
     print(r1)
     t3 = timeit.Timer('a.iSort()', 'from __main__ import a').timeit(1)
     a3time.append(t3)  #time for 1 time and add them to a timeset
Exemple #3
0
 def bubbleSort(self, r1):
     '''
     Function for bubble sort
     - makes multiple passes through a list.
     - compares adjacent items and exchanges those that are out of order. 
     '''
     #If there are n items in the list,
     #then there are n−1 pairs of items that need to be compared on the first pass.
     print("bSort")
     for passnum in range(len(r1) - 1, 0, -1):
         for i in range(passnum):  #Between the items for comparison:
             if r1[i] > r1[
                     i +
                     1]:  #If the former term is greater than the letter one:
                 temp = r1[i]
                 r1[i] = r1[i + 1]
                 r1[i + 1] = temp  #They will be swapped.
     print(r1)
     t1 = timeit.Timer('a.bSort()', 'from __main__ import a').timeit(1)
     a1time.append(t1)  #time for 1 time and add them to a timeset
Exemple #4
0
    def sSort(self, r1):
        '''
        Function for selection sort
        - improves on the bubble sort by making 
        only one exchange for every pass through the list. 
        - looks for the largest value as it makes a pass and, 
        after completing the pass, 
        places it in the proper location. 
        '''
        print("sSort")
        for fillslot in range(len(r1) - 1, 0, -1):
            positionOfMax = 0
        for location in range(1, fillslot + 1):
            if r1[location] > r1[positionOfMax]:
                positionOfMax = location

        temp = r1[fillslot]
        r1[fillslot] = r1[positionOfMax]
        r1[positionOfMax] = temp
        print(r1)
        t2 = timeit.Timer('a.sSort()', 'from __main__ import a').timeit(1)
        a2time.append(t2)  #time for 1 time and add them to a timeset
Exemple #5
0
#/usr/bin/env python
# -*- coding: utf-8 -*-
"""

"""

"""
pre:
    1. 

post:
    1.

"""
class Solution(object):
    pass

if __name__ == "__main__":
    from timeit import timeit
    n = 0
    ret = Solution()
    print(ret)
    import timeit
    timer = timeit.Timer("Solution().functionCall(%s)" % n, "from __main__ import Solution")
    print("timeConsume: %s" % timer.timeit(10))
Exemple #6
0
            sum_buffer = sum_buffer - old_tick + new_tick
            ma.append(sum_buffer/window_length)

    return ma

if __name__ == "__main__" :

    data_length = 100000    # 总数据量
    # 生成测试数据
    data = []
    for i in range(data_length):
        data.append(random.randint(1, 100))
    window_length = 500         # 移动均值的窗口
    test_times = 10         # 测试次数

    t_basic = timeit.Timer(functools.partial(window_mean_basic, data, window_length))
    t_numpy_wrong = timeit.Timer(functools.partial(window_mean_numpy_wrong, data, window_length))
    t_numpy_right = timeit.Timer(functools.partial(window_mean_numpy_right, data, window_length))
    t_numba = timeit.Timer(functools.partial(window_mean_numba, data, window_length))
    t_cache_algorithm = timeit.Timer(functools.partial(window_mean_cache, data, window_length))

    print("basic :\t" + str(t_basic.timeit(test_times)))
    print("numpy wrong :\t" + str(t_numpy_wrong.timeit(test_times)))
    print("numpy right :\t" + str(t_numpy_right.timeit(test_times)))
    print("numba :\t" + str(t_numba.timeit(test_times)))
    print("cache algorithm :\t" + str(t_cache_algorithm.timeit(test_times)))

    start = time.time()
    cores = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=cores)
    result = []
                return False

        p_0 = 0
        p_1 = 1
        p_s = 0
        while p_s!=len_s:
            if p[p_0]==s[p_s]:
                p_0+=1
                p_s+=1
            elif p[p_0]==".":
                pass

            p_s+=1
        
                    

    
    def cheat(self, s, p):
        import re
        return bool(re.match("^"+p+"$", s))

if __name__ == "__main__":
    from timeit import timeit
    s = "aaa"
    p = "ab*a"
    #from ipdb import set_trace; set_trace()
    ret = Solution().isMatch(s, p)
    print(ret)
    import timeit
    timer = timeit.Timer("Solution().isMatch('%s', '%s')" % (s, p), "from __main__ import Solution")
    print("timeConsume: %s" % timer.timeit(10))
Exemple #8
0
def _timed_func(func, *args):
    t = timeit.Timer()
    ret = func(*args)
    return (t.timeit(), ret)
Exemple #9
0
        length = len(nums)
        if length < 4:
            return max(nums)
        self.search(nums[:-1], tmp=0)
        self.search(nums[1:], tmp=0)
        self.search(nums[2:], tmp=0)

        return self.ret

    def search(self, nums, tmp):
        if nums:
            tmp += nums[0]
            self.search(nums[2:], tmp)
            self.search(nums[3:], tmp)
        else:
            if tmp > self.ret:
                self.ret = tmp


if __name__ == "__main__":
    from timeit import timeit
    nums = [
        226, 174, 214, 16, 218, 48, 153, 131, 128, 17, 157, 142, 88, 43, 37,
        157, 43, 221
    ]  #,191,68,206,23,225,82,54,118,111,46,80,49,245,63,25,194,72,80,143,55,209,18,55,122,65,66,177,101,63,201,172,130,103,225,142,46,86,185,62,138,212,192,125,77,223,188,99,228,90,25,193,211,84,239,119,234,85,83,123,120,131,203,219,10,82,35,120,180,249,106,37,169,225,54,103,55,166,124]
    ret = Solution().rob(nums)
    print(ret)
    import timeit
    timer = timeit.Timer("Solution().rob(%s)" % nums,
                         "from __main__ import Solution")
    print("timeConsume: %s" % timer.timeit(10))
Exemple #10
0
    r2.append(int(random.random() * 20000))
r3 = []
for i in range(0, 30000):
    r3.append(int(random.random() * 30000))
r4 = []
for i in range(0, 40000):
    r4.append(int(random.random() * 40000))
r5 = []
for i in range(0, 50000):
    r5.append(int(random.random() * 50000))

a = sortingAlgorithms()
a.bubbleSort(r1)
a.sSort(r1)
a.iSort(r1)
timeit.Timer(a.bSort).timeit()
timeit.Timer(a.sSort).timeit()
timeit.Timer(a.iSort).timeit()
a.output()
#Make a title for the graph
plt.title("Sorting Algorithms")
#Plot the time(x-value(s),y-value(s),the style(red dots) of graph)
plt.plot(r1, [a1time, a2time, a3time], 'ro:')
#Change the size of the axis label:
ax = plt.gca()
for label in ax.xaxis.get_ticklabels():
    label.set_fontsize(20)
for label in ax.yaxis.get_ticklabels():
    label.set_fontsize(20)
myfont = matplotlib.font_manager.FontProperties(
    fname='C:/Windows/Fonts/simsun.ttc', size=24)