SQLite Efficiency detection

Source: Internet
Author: User

After compiling is of course use SQLite, trade rushed to test to create a database, insert data, only a few data, found, really good database, and later the amount of data increased to 10000 when, found, how so slow, then I can not stand, online a check, It turns out that if the transaction is not used in the case of each execution will automatically represent a transaction for the database to read and write, rather than in memory operation, think that this must be very slow, and then add to their own business, it is really surprising that the increase several times must be good thousands of times. Excited....

Post the Code:

#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 ("record contains%d fields \ n", n_column);

for (int i = 0; i < n_column; i + +)
{
printf ("Field Name:%s field value:%s\n", Column_name[i], column_value[i]);
}

printf ("------------------\ n");
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 (+), sex integer,age integer)", NULL, NULL, &errmsg);

if (Result!=sqlite_ok)
printf ("Failed to create TABLE, error code:%d, error reason:%s\n", 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 ("Insert record failed, error code:%d, error reason:%s\n", 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 ("Database initialization time:%dms\r\n", timeGetTime ()-dwnow);


return 0;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.