-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_array.hpp
executable file
·85 lines (64 loc) · 1.94 KB
/
test_array.hpp
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
#ifndef _TEST_ARRAY_H
#define _TEST_ARRAT_H
#include <string>
#include <array>
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstdlib> // qsort binarysreach, NULL
#include "pub.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::array;
namespace jj01
{
void test_array()
{
cout << "\ntest_array()......... \n";
array<long, ASIZE> c;
//srand((unsigned)time(NULL));
clock_t timeStart = clock();
for (long i = 0; i < ASIZE; ++i) {
c[i] = rand() % 65535;
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "array.size()= " << c.size() << endl;
cout << "array.front()= " << c.front() << endl;
cout << "array.back()= " << c.back() << endl;
cout << "array.data()= " << c.data() << endl;
long target = get_a_target_long();
timeStart = clock();
cout << "-------------------before qsort: --------------" << endl;
/*
for (long i = 0; i < ASIZE; ++i)
{
cout << "The " << i << " element is:" << c[i] << endl;
}
*/
timeStart = clock();
qsort(c.data(), ASIZE, sizeof(long), compareLongs);
cout << "qsort(), milli-seconds: " << (clock() - timeStart) << endl;
cout << "---------------------after qsort:------------------ " << endl;
//timeStart = clock();
/*
for (long i = 0; i < ASIZE; ++i)
{
cout << "The " << i << " element is:" << c[i] << endl;
}
*/
cout << "array.size()= " << c.size() << endl;
cout << "array.front()= " << c.front() << endl;
cout << "array.back()= " << c.back() << endl;
cout << "array.data()= " << c.data() << endl;
timeStart = clock();
long* pItem = (long*)bsearch(&target, (c.data()), ASIZE, sizeof(long), compareLongs);
cout << "bsearch(), milli-seconds: " << (clock() - timeStart) << endl;
if (pItem != NULL)
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
}
#endif