-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_test.c
196 lines (168 loc) · 4 KB
/
insert_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <sqlite3.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
// 使用数据预准备
void test_fuc1(sqlite3 *db, int N)
{
for (int i = 0; i < N; ++i)
{
std::stringstream ssm;
ssm << "insert into testInfo values(" << i << "," << i * 2 << "," << i / 2 << "," << i * i << ")";
sqlite3_exec(db, ssm.str().c_str(), 0, 0, 0);
}
}
//3
float test_fuc3(sqlite3 *db, int N)
{
clock_t t1 = clock();
clock_t t2 = clock();
float ret = (t2 - t1) / 1.;
std::string sql = "insert into testInfo values";
for (int i = 0; i < N; ++i)
{
sql += "," + "," + "," + ")"
}
return ret;
}
//4
float test_fuc4(sqlite3 *db, int N)
{
clock_t t1 = clock();
clock_t t2 = clock();
float ret = (t2 - t1) / 1.;
return ret;
}
// 使用数据预准备
void test_fuc5(sqlite3 *db, int N)
{
sqlite3_stmt *stmt;
const char *sql = "insert into testInfo values(?,?,?,?)";
sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, 0);
for (int i = 0; i < N; ++i)
{
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, i);
sqlite3_bind_int(stmt, 2, i * 2);
sqlite3_bind_int(stmt, 3, i / 2);
sqlite3_bind_double(stmt, 4, i * i);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
/**
* 计算两个时间的间隔,得到时间差
* @param struct timeval* resule 返回计算出来的时间
* @param struct timeval* x 需要计算的前一个时间
* @param struct timeval* y 需要计算的后一个时间
* return -1 failure ,0 success
**/
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
int nMsec = 0;
if (x->tv_sec > y->tv_sec)
return -1;
if ((x->tv_sec == y->tv_sec) && (x->tv_usec > y->tv_usec))
return -1;
result->tv_sec = (y->tv_sec - x->tv_sec);
result->tv_usec = (y->tv_usec - x->tv_usec);
if (result->tv_usec < 0)
{
result->tv_sec--;
result->tv_usec += 1000000;
}
nMsec = result->tv_sec * 1000000 + result->tv_usec;
return nMsec;
}
int main(int argc, char **argv)
{
sqlite3 *db;
int N = 0;
int fuc_num = 0;
float cost_time = 0;
if (remove("testdb.db") == 0)
printf("Removed %s.", "testdb.db");
else
perror("remove");
sqlite3_open("testdb.db", &db);
sqlite3_exec(db, "drop table if exists testInfo", 0, 0, 0);
sqlite3_exec(db, "create table testInfo(id integer,x integer,y integer ,weight real)", 0, 0, 0);
sqlite3_exec(db, "PRAGMA synchronous = OFF; ", 0, 0, 0);
std::cout << "input number of date:";
std::cin >> N;
std::cout << "func number:";
std::cin >> fuc_num;
switch (fuc_num)
{
case 1:
{
//sqlite3_exec(db, "PRAGMA synchronous = FULL; ", 0, 0, 0);
struct timeval start, stop, diff;
gettimeofday(&start, 0);
//
test_fuc1(db, N);
//
gettimeofday(&stop, 0);
float cost_time = timeval_subtract(&diff, &start, &stop);
printf("1:总计用时:%f 毫秒\n", cost_time);
break;
}
case 2:
{
struct timeval start, stop, diff;
gettimeofday(&start, 0);
//
sqlite3_exec(db, "begin;", 0, 0, 0);
test_fuc1(db, N);
sqlite3_exec(db, "commit;", 0, 0, 0);
//
gettimeofday(&stop, 0);
float cost_time = timeval_subtract(&diff, &start, &stop);
printf("2:总计用时:%f 微秒\n", cost_time);
break;
}
case 3:
{
cost_time = test_fuc3(db, N);
break;
}
case 4:
{
cost_time = test_fuc4(db, N);
break;
}
case 5:
{
struct timeval start, stop, diff;
gettimeofday(&start, 0);
//
sqlite3_exec(db, "begin;", 0, 0, 0);
test_fuc5(db, N);
sqlite3_exec(db, "commit;", 0, 0, 0);
//
gettimeofday(&stop, 0);
float cost_time = timeval_subtract(&diff, &start, &stop);
printf("5:总计用时:%f 微秒\n", cost_time);
break;
}
default:
{
struct timeval start, stop, diff;
gettimeofday(&start, 0);
sleep(10);
gettimeofday(&stop, 0);
float cost_time = timeval_subtract(&diff, &start, &stop);
printf("总计用时:%f 微秒\n", cost_time);
break;
}
}
sqlite3_close(db);
std::cout << "total:" << N << "cost time: " << cost_time << "s" << std::endl;
return 1;
}