3 搜索|排序&复杂度分析(3)

本文介绍了一种名为Profiler的工具,用于度量算法性能,特别是针对选择排序算法。Profiler通过计数比较和交换操作来衡量算法的效率,并提供运行时间等信息。它还支持在每次交换后打印列表,方便跟踪算法执行过程。降低算法复杂度通常比优化代码更有效。
摘要由CSDN通过智能技术生成

算法探查器:

Profiling 通过统计指令或执行的时间来度量一个算法的性能

'''
algorithms.py
Algorithms configured for profiling
'''

def selectionSort(lyst, profiler):
    i = 0
    while i < len(lyst) - 1:
        minIndex = i
        j = i + 1
        while j < len(lyst):
            profiler.comparison()
            if lyst[j] < lyst[minIndex]:
                minIndex = j
            j += 1
        if minIndex != i:
            swap(lyst, minIndex, i, profiler)
        i += 1

def swap(lyst, i, j, profiler):
    profiler.exchange()
    temp = lyst[i]
    lyst[i] = lyst[j]
    lyst[j] = temp
'''
profiler.py

Example use:
from profiler import Profiler
from algorithms import selectionSort

p = Profiler()
p.test(selectionSort, size=15, comp=True, exch=True, trace=True)
'''

import time
import random

class Profiler(object):
    def test(self, function, lyst=None, size=10, unique=True,
             comp=True, exch=True, trace=False):
        '''
        function: the algorithm being profiled
        target: the search target if profiling a search
        lyst: allows the caller to use her list
        size: the size of the list, 10 by default
        unique: if True, list contains unique integers
        comp: if True, count comparisons
        exch: if True, count exchanges
        trace: if True, print the list after each exchange
        '''
        
        self._comp = comp
        self._exch = exch
        self._trace = trace
        if lyst != None:
            self._lyst = lyst
        elif unique:
            self._lyst = [i for i in range(1, size+1)]
            random.shuffle(self._lyst)
        else:
            self.lyst = []
            for count in range(size):
                self._lyst.append(random.randint(1, size))
        
        self._exchCount = 0
        self._cmpCount = 0
        self._startClock()
        function(self._lyst, self)
        self._stopClock()
        print(self)
    
    def exchange(self):
        if self._exch:
            self._exchCount += 1
        if self._trace:
            print(self._lyst)
    
    def comparison(self):
        if self._comp:
            self._cmpCount += 1
    
    def _startClock(self):
        self._start = time.time()
    
    def _stopClock(self):
        self._elapsedTime = round(time.time() - self._start, 3)
    
    def __str__(self):
        result = 'Problem size: '
        result += str(len(self._lyst)) + '\n'
        result += 'Elapsed time: '
        result += str(self._elapsedTime) + '\n'
        if self._comp:
            result += 'Comparisons: '
            result += str(self._cmpCount) + '\n'
        if self._exch:
            result += 'Exchanges: '
            result += str(self._exchCount) + '\n'
        return result

 通常减少算法复杂度的阶的做法,比通过调整代码来增强其性能要好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值