回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我的代码有一个恼人的问题,可能是我做错了什么,因为我的Python
实现速度快得多!在</p>
<P> C++实现问题:</P>
<ol>
<li>迭代“HKEY_CLASSES_ROOT”需要大量ram,我想这是因为c++实现使用了很多变量。<strong><em>固定</em></strong></li>
<li>它也很慢,比python植入代码的速度慢得多</li>
<li>当试图迭代HKEY_CLASSES_ROOT<strong><em>Fixed</em></strong>时,代码甚至会慢下来</li>
</ol>
<p>新问题:</p>
<ol>
<li>多亏了<strong>Nam Nguyen</strong>我了解了是什么导致了我的代码中的漏洞,并直接影响了执行时间,下面的代码是固定的。为什么c++实现的运行速度和我的python实现一样快?在</li>
</ol>
<P> C++实现:</P>
<pre><code>#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std;
#define MAX_KEY_LENGTH 255
int RecurseOpenRegEx(HKEY hkey, string subKey = "",string search = "nothing", DWORD sum = 0)
{
TCHAR csubKey[MAX_KEY_LENGTH];
DWORD nSubKeys = 0;
DWORD pathLength = MAX_PATH;
TCHAR storeKeyName[MAX_KEY_LENGTH];
DWORD keyLength;
HKEY hKey = hkey; //somehow i need to reassign HKEY, otherwise it won't pass it with the function, this is bigger than me tough...
const char * ccsearch = search.c_str();
const char * ccsubKey;
if (subKey != "")
{
ccsubKey = subKey.c_str();
copy(subKey.begin(), subKey.end(),csubKey); //convert string to TCHAR
}
if (RegOpenKeyEx(hkey, ccsubKey, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
{
if (RegQueryInfoKey(hkey, csubKey, &pathLength, NULL,&nSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
sum += nSubKeys;
for (DWORD subKeyIndex = 0; subKeyIndex < nSubKeys; subKeyIndex++)
{
keyLength = MAX_KEY_LENGTH;
if (RegEnumKeyEx(hkey, subKeyIndex, storeKeyName, &keyLength, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
string sKeyName = storeKeyName; //Convert TCHAR to string explicitly
if (subKey != "")
{
sKeyName = subKey + "\\" + sKeyName;
}
sum += RecurseOpenRegEx(hKey, sKeyName);
}
}
}
}
RegCloseKey(hkey); //Now closing the right key
return sum;
}
int main()
{
cout << "sum of all keys: " << RecurseOpenRegEx(HKEY_LOCAL_MACHINE);
return 0;
}
</code></pre>
<p>Python实现:</p>
^{pr2}$