羽毛球

生活在别处

导航

<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

统计

留言簿(29)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

读写注册表和读写文件哪种快

读写注册表中的一个键值和读写一个小文件中(<1K)的一个值,那个快?
循环10000,测试结果如下:  millisecond
Read Registry Write Registry Read File Write File
453  421 811 14140

读注册表比读文件大约快2倍
写注册表比写文件快了30多倍
可能windows对注册表的操作做了很多优化,譬如写,可能直接写到内存中,而不是真正放到磁盘中,这样会快很多。

测试代码如下:

// regVSfile.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<windows.h>

const int TEST_TIMES = 10000;


void TestReadRegistry()
{
    
for (int i = 0; i < TEST_TIMES; i++)
    
{
        HKEY m_hKey 
= 0;
        
long retcode = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "software\xxxxx\abc",
            
0"", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, NULL);

        
const int MAX_KEY_VALUE_LENGTH = 256;
        
char szTmp[MAX_KEY_VALUE_LENGTH+1];
        DWORD dwLen 
= sizeof(szTmp) -1;
        DWORD dwType 
= REG_SZ;
        retcode 
= RegQueryValueExA(m_hKey, "InitialCollect", NULL, &dwType, (LPBYTE)szTmp, &dwLen);
        
if (0 != retcode)
        
{
            MessageBox(
0"abc""error", MB_OK);
        }

        RegCloseKey(m_hKey);
    }

}


void TestWriteRegistry()
{
    
for (int i = 0; i < TEST_TIMES; i++)
    
{
        HKEY m_hKey 
= 0;
        
long retcode = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "software\xxxxx\abc",
            
0"", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, NULL);
        retcode 
= RegSetValueExA(m_hKey, "InitialCollect", NULL, REG_SZ, (LPBYTE)"abc"3);
        
if (0 != retcode)
        
{
            MessageBox(
0"abc""error", MB_OK);
        }

        RegCloseKey(m_hKey);
    }

}


void TestReadFile()
{
    
for (int i = 0; i < TEST_TIMES; i++)
    
{
        HANDLE hFile
= CreateFile("D:\vctest\regVSfile\test.txt", GENERIC_READ, 00, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        
const int MAX_KEY_VALUE_LENGTH = 256;
        
char szTmp[MAX_KEY_VALUE_LENGTH+1];
        DWORD dwLen 
= sizeof(szTmp) -1;
        DWORD dwRead 
= 0;
        BOOL bRet 
= ReadFile(hFile, szTmp, dwLen, &dwRead, 0);
        
if (!bRet)
        
{
            MessageBox(
0"abc""error", MB_OK);
        }

        CloseHandle(hFile);
    }

}


void TestWriteFile()
{
    
const char* szString = "Michael vckbase haha";
    
int nLen = strlen(szString);
    
for (int i = 0; i < TEST_TIMES; i++)
    
{
        HANDLE hFile
= CreateFile("D:\vctest\regVSfile\test.txt", GENERIC_WRITE, 00, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        DWORD dwWritten 
= 0;
        BOOL bRet 
= WriteFile(hFile, szString, nLen, &dwWritten, 0);
        
if (!bRet)
        
{
            MessageBox(
0"abc""error", MB_OK);
        }

        CloseHandle(hFile);
    }

}

int main(int argc, char* argv[])
{
    DWORD dwBegin 
= GetTickCount();    
    
//TestReadRegistry();
    TestWriteRegistry();
    
//TestReadFile();
    
//TestWriteFile();
    DWORD dwInterval = GetTickCount() - dwBegin;
    printf(
"Hello World: %d miliseconds! ", dwInterval);
    
return 0;
}


 

posted on 2006-07-09 17:43 Michael 阅读(3502) 评论(3)  编辑 收藏

评论

# re: 读写注册表和读写文件哪种快 2006-07-10 10:49 xulion

这样测试好像有点无聊吧,注册表本身就是文件,存放在系统system32\config目录下,windows启动后,此文件就一直处于open状态,而在你的测试中,竟然在每次循环中往复打开文件。。。
这样肯定慢了。。。。

# re: 读写注册表和读写文件哪种快 2006-07-12 16:10 popyzl

you can try to open the file once and do it again

# 370830951 2007-04-29 01:18 田乐时

我的qq不见了我的同学还有战友都找不到了我好急哦

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]