我正在尝试优化 pyOpenCL 程序。出于这个原因,我想知道是否有办法分析程序并查看大部分时间需要在哪里。
你知道如何解决这个问题吗?
提前
致谢安迪
编辑:例如,用于 CUDA 的 nvidias nvprof 可以用于 pyCuda,但不适用于 pyOpenCL。
我在一些代码上运行了cprofile,其中包括执行大部分工作的几个线程.当我查看分析的输出时,我看到没有记录在线程内调用的所有函数.我确信他们被称为,因为他们做的事情很容易看到,如写入数据库等.
cProfile不会分析线程吗?我错过了什么吗?
我正在运行我的代码,包括查找平均值.在~600万行CSV(ssm_resnik.txt)中,第一行是一个引用,第二行是另一行,第三行是2个引用之间的"距离".这些距离由生物标准任意定义,对此问题不重要.大多数参考文献将与大多数参考文献相对应,因此具有超过6百万行的巨大CSV.在另一个CSV(all_spot_uniprot.txt)中,我有~3600个点(第一列),每个点都有一个或多个引用(第三列).这些值与巨大的CSV相同.我需要将第二个文件的3600个spot ref中的每一个与同一个文件中的所有其他3600-1 ref进行比较.所有可能的组合(如果存在)都位于第一个巨大的CSV文件(ssm_resnik.txt)中.对于all_spot_uniprot.txt,每个2 spot ref将作为对应引用的迭代器(在第三列中),并将迭代巨大的CSV文件,如果存在,则显示两个"VS"引用的值.
我的代码有什么问题?那么......每次迭代10秒,3600*3600*10 = 129.600.000秒= 1500天(差不多5年).这发生在我的核心i3中,但在mac中.下面是我的代码和每个文件的一部分.请指教.有任何代码设计缺陷吗?有一些方法可以减少计算时间吗?提前致谢...
import csv
spot_to_uniprot=open('all_spot_uniprot.txt', 'rbU')
STU=csv.reader(spot_to_uniprot, delimiter='\t')
uniprot_vs_uniprot=open('ssm_resnik.txt', 'rbU')
allVSall= csv.reader(uniprot_vs_uniprot, delimiter='\t')
recorder=open('tabela_final.csv', 'wb')
fout=csv.writer(recorder, delimiter='\t', quotechar='"')
dict_STU={} #dicionário 'spot to uniprot'
dict_corresp={} #for each pair of uniprot ref as key and as value
#a list of lists with the first list as one spot and the second list is the spot+1
dict_corresp_media={}##average of one spot to other
total_correspondencias_x_y=[]
_lista_spot=[]
lista_spot=[]
lista_temp=[]
lista_CSV=[]
for a in STU:
_lista_spot.append(int(a[0]))
if a[0] not in …Run Code Online (Sandbox Code Playgroud) 我编写了一个Python脚本,但是运行它比我预期的要花费更长的时间,并且我没有明显的候选者在脚本占用运行时的特殊行.
我可以在代码中添加任何内容来检查每条线路的运行时间吗?
非常感谢.
我刚刚在python中编写了我的第一个程序,我已经将所有函数都写在一个模块中,我只是通过将输入文件作为参数从命令行执行它并且它起作用.但是当我给出一个大数据集时,我的程序会持续运行一段时间.现在我的下一步是找到哪个功能在我的模块中花费更多时间.我可以得到整个程序所花费的时间,但我需要单独的每个功能.
我试图理解python中的timeit和profile模块,但根据我的理解,他们给出了特定函数所花费的时间.有没有办法知道我的模块中的每个功能所用的时间作为统计数据(一次全部)?
提前致谢.
我有一个Python应用程序,我希望在运行时监视标准随机模块中函数的调用次数; 有什么好方法可以做到这一点,还是我必须"手动"做到这一点?
我用 Python 编写了代码来计算 10000 以下的友好数字的总和:
def amicable(a, b):
total = 0
result = 0
for i in range(1, a):
if a % i == 0:
total += i
for j in range(1, b):
if b % j == 0:
result += j
if total == b and result == a:
return True
return False
sum_of_amicables = 0
for m in range (1, 10001):
for n in range (1, 10001):
if amicable(m, n) == True and m != n:
sum_of_amicables = …Run Code Online (Sandbox Code Playgroud) 我使用以下代码来实现一个函数,该函数在字符串s中查找字符串p的所有字符串.
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
ans = list()
pcnt = collections.Counter(p)
for i in range(len(s)):
if collections.Counter(s[i:i+len(p)]) == pcnt:
ans.append(i)
return ans
Run Code Online (Sandbox Code Playgroud)
当在大长度输入字符串s上运行时,它在在线代码测试系统中给出了"超出时间限制"的错误.但是,以下代码将不会出现此类问题:
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
ls, lp = len(s), len(p)
cp = collections.Counter(p)
cs = collections.Counter()
ans = []
for i in range(ls):
cs[s[i]] += 1
if i >= lp:
cs[s[i - lp]] -= …Run Code Online (Sandbox Code Playgroud) 我一直在做一些计算机图形学项目,这些项目围绕使用编写的开源库和 C/C++ 进行,然后将它们转换为 python 的包装器。
我想知道将其转换为纯 C/C++ 所带来的性能提升是否值得花费大量时间来重写代码库。
我也知道 C/C++ 几乎总是比 python 快,但考虑到这些库已经是 C/C++ 包装器,我不确定我应该期望多少性能提升。我并不是在寻找确切的答案,因为这在很大程度上取决于具体情况,但如果有人有一般的经验法则,那就太好了!
HackerRank 的请求:
\n\n\n如果客户在特定日期的支出金额大于或等于 2\xc3\x97 客户在过去几天的平均支出,他们会向客户发送有关潜在欺诈的通知。银行不会向客户发送任何通知,直到他们至少拥有前几天的交易数据的跟踪数据。
\n给定跟踪天数d和客户在n天期间的每日总支出,确定客户在所有n天内收到通知的次数。
\n
我的代码可以解决问题,但是对于大型测试用例来说有时间限制。我的代码无法通过时间限制要求。我的代码实际上很短:
\nfrom statistics import median\n\nfirst_multiple_input = input().rstrip().split()\nn = int(first_multiple_input[0])\nd = int(first_multiple_input[1])\nexpenditure = list(map(int, input().rstrip().split()))\ncount=0\nfor i in range(len(expenditure)-d):\n if expenditure[d+i] >= 2*median(expenditure[i:d+i]) :\n count+=1\nprint( count)\nRun Code Online (Sandbox Code Playgroud)\n请指出造成延误的原因以及如何改善。
\n帮助理解代码的小测试用例:
\n9 5 expenditure[] size n =9, d = 5\n2 3 4 2 3 6 8 4 5 expenditure = [2, 3, 4, 2, 3, 6, 8, 4, 5]\nRun Code Online (Sandbox Code Playgroud)\n python ×9
performance ×3
python-3.x ×2
c++ ×1
counter ×1
cprofile ×1
cython ×1
numbers ×1
opencl ×1
optimization ×1
profiling ×1
pyopencl ×1
python-2.7 ×1
random ×1
time ×1