sp4*_*497 2 c c++ algorithm optimization file
即使文件大小大到4GB或更多,如何以优化的方式查找字母AZ的出现(无视情况)?在C++/C中可能有哪些不同的实现?
一个实现是:
伪代码
A[26]={0}
loop through each character ch in file
If isalpha(ch)
A[tolower(ch)-'A']+ = 1
End If
end loop
Run Code Online (Sandbox Code Playgroud)
我认为没有太多优化.
不要计算tolower()-'A'每个元素,只计算每个字符的出现次数(在char[256]累加器中),然后进行大小写感知计算(可能更高效或不高效,只需尝试).
一定要使用缓冲输入(fopen也许可以分配更大的缓冲区setvbuf).
例如:
acum[256]={0}
loop through each character 'c' in file
acum[c]++
end loop
group counts corresponding to same lowercase/uppercase letters
Run Code Online (Sandbox Code Playgroud)
另外,请记住,这假设ASCII或派生(一个八位字节=一个字符)编码.