相关疑难解决方法(0)

如何在CPython中实现string.find?

我想知道字符串上的'find'方法是用线性搜索实现的,还是python做了更复杂的事情.Python文档没有讨论实现细节,所以http://docs.python.org/library/stdtypes.html没有任何帮助.有人可以指点我相关的源代码吗?

python

9
推荐指数
1
解决办法
3783
查看次数

Python字符串搜索效率

对于非常大的字符串(跨越多行),使用Python的内置字符串搜索或拆分大字符串(可能打开\n)并迭代搜索较小的字符串会更快吗?

例如,对于非常大的字符串:

for l in get_mother_of_all_strings().split('\n'):
 if 'target' in l:
   return True
return False
Run Code Online (Sandbox Code Playgroud)

要么

return 'target' in get_mother_of_all_strings()
Run Code Online (Sandbox Code Playgroud)

python performance

5
推荐指数
1
解决办法
9735
查看次数

这是搜索子字符串的最有效方法吗?

我正在处理一些代码,这些代码返回一个代码来指示它们的用户类型(例如"A","B","C","D"等).每个代码对应于特定的角色和/或范围(例如,跨整个应用程序或仅针对正在处理的对象).

在我正在查看的一些代码中,我看到调用以检查用户的代码是否是其中之一,以便允许它们执行某些操作.所以我看到如下调用:

//"B" would come from the database
string userCode = "B";

//some more work...

//if the user's code is either A or C...
if("AC".IndexOf(userCode) >= 0) {
  //do work that allows the user to progress
} else {
  //notify user they can't do this operation
}
Run Code Online (Sandbox Code Playgroud)

这是执行此检查的有效方式吗?有更有效的方法吗?

提前致谢!

c# performance

4
推荐指数
1
解决办法
663
查看次数

高效搜索海量文件中的字符串

我发现了这个想法的变体,但没有一个可以让我(对 python 非常陌生)到达我需要的地方。

这是场景:

  1. 我有一个巨大的 27 演出,hashfile.txt由单独的字符串组成。
  2. 我需要逐行解析这个文件,在另一个不太大(~800mb)的addresses.txt文件中搜索匹配项
  3. 当找到匹配时,这需要写入 outfile.txt

我当前的代码已尽我所能优化,但只有 150 行/秒左右。考虑到我有超过 15 亿行hashfile.txt,任何优化都会有所帮助。

fin = 'hashed.txt'
nonzeros = open('addrOnly.txt', 'r')
fout = open('hits.txt', 'w')
lines = nonzeros.read()
i = 0
count = 0

with open(fin, 'r') as f:
    for privkey in f:
            address = privkey.split(", ")[0]
            if address in lines:
                    fout.write(privkey)
            i = i+1
            if i%100 == 0:
                    count = count + 100
                    print "Passed: " + str(count)
Run Code Online (Sandbox Code Playgroud)

python optimization search large-files

4
推荐指数
2
解决办法
3011
查看次数

Python:通过嵌入复杂的算法改进子字符串搜索

我正在扩展我以前的问题python高效子字符串搜索,

我有兴趣提高子串搜索实现的性能,

我前一个问题的一些答案指出子串搜索是通过使用受BM算法启发的fastsearch实现的,这里是源代码

更多的答案指向了Boyer-Moore算法,Rabin-Karp算法的python实现.

将c代码作为使用这些算法(BM,Rabin-Karp)的子串搜索的良好实现嵌入是否有效?

c python algorithm performance substring

1
推荐指数
1
解决办法
451
查看次数

标签 统计

python ×4

performance ×3

algorithm ×1

c ×1

c# ×1

large-files ×1

optimization ×1

search ×1

substring ×1