line_profiler and memory_profiler

  • line_profile 的用法
    pip install line_profiler

用法1, 导入line_profiler+装饰器profile

from line_profiler import LineProfiler

from collections import defaultdict

class Solution1(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        if strs == "":
            return []
        hashmap = defaultdict(list)
        for eles in strs:   # O(N)
            hashmap[tuple(sorted(eles))].append(eles) # O(klogk)
        return list(hashmap.values())


class Solution(object):
    def groupAnagrams(self, strs):
        if strs == "":
            return []
        hashmap1 = defaultdict(list)
        for ele in strs:  # O(N)
            dic2 = dict().fromkeys(range(26), 0)  # O(k)
            for char in ele:  # O(k)
                dic2[ord(char)-ord('a')] = \
                    dic2.get(ord(char)-ord('a'), 0) +1
            val= tuple(dic2.values())
            hashmap1[val].append(ele)
        # Solution1().groupAnagrams(strs)
        return list(hashmap1.values())

strs =  ["eat", "tea", "tan", "ate", "nat", "bat"]

# ♠

lp = LineProfiler()
lp.add_function(Solution1.groupAnagrams) # 被引用函数需要声明才显示细节 
lp_wrapper = lp(Solution().groupAnagrams)  # 被显示的函数
lp_wrapper(strs)  # 参数传入
lp.print_stats()  # 打印喽

用法2, 装饰器,被引用函数不显示细节
kernprof -l -v test.py

from collections import defaultdict

class Solution(object):
    @profile
    def groupAnagrams(self, strs):
        if strs == "":
            return []
        hashmap1 = defaultdict(list)
        for ele in strs:  # O(N)
            dic2 = dict().fromkeys(range(26), 0)  # O(k)
            for char in ele:  # O(k)
                dic2[ord(char)-ord('a')] = \
                    dic2.get(ord(char)-ord('a'), 0) +1
            val= tuple(dic2.values())
            hashmap1[val].append(ele)
        
        return list(hashmap1.values())


strs =  ["eat", "tea", "tan", "ate", "nat", "bat"]
s = Solution()
rt = s.groupAnagrams(strs)
print(rt)
  • memory_profiler用法
    pip install memory_profiler
    用法1,
    导入from memory_profiler import profile,
    同时写上装饰器。测试完了需要删引入和装饰器
# ♠1
from memory_profiler import profile

from collections import defaultdict


class Solution(object):

    @profile
    def groupAnagrams(self, strs):
        if strs == "":
            return []
        hashmap1 = defaultdict(list)
        for ele in strs:  # O(N)
            dic2 = dict().fromkeys(range(26), 0)  # O(k)
            for char in ele:  # O(k)
                dic2[ord(char)-ord('a')] = \
                    dic2.get(ord(char)-ord('a'), 0) +1
            val= tuple(dic2.values())
            hashmap1[val].append(ele)
        return list(hashmap1.values())


strs =  ["eat", "tea", "tan", "ate", "nat", "bat"]
s = Solution()
rt = s.groupAnagrams(strs)
print(rt)

用法2,
不引入模块memory_profiler,只写装饰器profile
执行命令: python -m memory_profiler script.py

from collections import defaultdict


class Solution(object):

    @profile
    def groupAnagrams(self, strs):
        if strs == "":
            return []
        hashmap1 = defaultdict(list)
        for ele in strs:  # O(N)
            dic2 = dict().fromkeys(range(26), 0)  # O(k)
            for char in ele:  # O(k)
                dic2[ord(char)-ord('a')] = \
                    dic2.get(ord(char)-ord('a'), 0) +1
            val= tuple(dic2.values())
            hashmap1[val].append(ele)
        return list(hashmap1.values())


strs =  ["eat", "tea", "tan", "ate", "nat", "bat"]
s = Solution()
rt = s.groupAnagrams(strs)
print(rt)

你可能感兴趣的:(PYTHON3)