• sqlite效率探测


    在编译后当然就是使用sqlite,贸贸然去测试了一下创建数据库,插入数据,仅几条数据,发现,真不错的数据库,后来把数据量提高到10000的 时候,发现,怎么这么慢,后来自己都受不了,网上一查,原来是如果在没有使用事务的情况下每次执行都会自动代表一次事务进行数据库读写,而不是在内存中操 作,想想看这样必然很慢,后来自己把事务添加上去,真的很惊讶,提高数度肯定有好几千倍。兴奋。。。。

    把代码帖一下:

    #include <iostream>
    #include <tchar.h>
    #include <windows.h>
    #include <MMSystem.h>
    #pragma comment(lib,"winmm.lib")

    #define  TEST_TIMES 10000
    #define  ONE_M      1048576

    #define  TEST_SELECT
    #define  USE_TRANSACTION

    #include "sqlite3.h"

    typedef int(*sqlite3_callback)(void*,int,char**,char**);

    char Name[4][32] = {"david","boluns","guanzhongdaoke","wangba"};

    char Sex[2][8]  = {"boy","girl"};

    int SleectCB(void* para,int n_column,char** column_value,char** column_name )
    {
     printf("记录包含%d个字段 ", n_column );

     for(int i = 0 ; i < n_column; i ++ )
     {
      printf("字段名:%s  字段值:%s ", column_name[i], column_value[i] );
     }

     printf("------------------ ");  
     return 0;

    }

    int sqlite_main()
    {
     sqlite3 * db = 0;
     int result = 0;
     char * errmsg = 0;
     
    #ifdef TEST_SELECT

     remove("test.db");

     result = sqlite3_open("test.db", &db );

     if( result !=SQLITE_OK)
      return -1;

     result = sqlite3_exec( db,"create table Info( ID integer primary key , name nvarchar(32) ,sex integer,age integer)", NULL, NULL, &errmsg );

     if(result !=SQLITE_OK)
      printf("创建表失败,错误码:%d,错误原因:%s ", result, errmsg );
      
    #ifdef USE_TRANSACTION
     result = sqlite3_exec( db,"begin;", 0, 0, &errmsg ); 
    #endif

     char sSQL[128] = {0};
     
     for (int i=0;i<TEST_TIMES;i++)
     {
      memset(sSQL,0,128);
      sprintf(sSQL,"insert into Info values (%d,'%s','%s',%d)",i,Name[rand()%4],Sex[rand()%2],rand()%50+1);

      result = sqlite3_exec( db,sSQL, 0, 0, &errmsg );

      if(result !=SQLITE_OK)
       printf("插入记录失败,错误码:%d,错误原因:%s ", result, errmsg );
     }
     
    #ifdef USE_TRANSACTION
     result = sqlite3_exec( db,"commit;", 0, 0, &errmsg );
    #endif

    #else

     result = sqlite3_open("test.db", &db );

     if( result !=SQLITE_OK)
      return -1;

     result = sqlite3_exec( db,"select * from Info", SleectCB, NULL, &errmsg );

    #endif

     sqlite3_close( db );

     return 0;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
     DWORD dwNow = timeGetTime();

     sqlite_main();

     printf("数据库初始化用时:%dms ",timeGetTime()-dwNow);


     return 0;

  • 相关阅读:
    [LeetCode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
    [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
    Notepad++ Shortcuts 快捷键
    IplImage 与 QImage 相互转换
    [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
    QMessageBox 使用方法
    Qt5 和 Qt4 的一些改动和不同
    Qt5.4 VS2010 Additional Dependancies
    [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
    [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
  • 原文地址:https://www.cnblogs.com/ejllen/p/3810274.html
Copyright © 2020-2023  润新知