1 | #include <iostream> |
2 | #include <iomanip> |
3 | |
4 | #include <common/Types.h> |
5 | |
6 | #include <IO/ReadHelpers.h> |
7 | #include <IO/WriteHelpers.h> |
8 | #include <IO/WriteIntText.h> |
9 | #include <IO/WriteBufferFromVector.h> |
10 | #include <IO/WriteBufferFromString.h> |
11 | #include <IO/AsynchronousWriteBuffer.h> |
12 | #include <Compression/CompressedWriteBuffer.h> |
13 | #include <Compression/CompressedReadBuffer.h> |
14 | |
15 | #include <Common/Stopwatch.h> |
16 | |
17 | |
18 | static UInt64 rdtsc() |
19 | { |
20 | #if defined(__x86_64__) |
21 | UInt64 val; |
22 | __asm__ __volatile__("rdtsc" : "=A" (val) :); |
23 | return val; |
24 | #else |
25 | // TODO: make for arm |
26 | return 0; |
27 | #endif |
28 | } |
29 | |
30 | |
31 | int main(int argc, char ** argv) |
32 | { |
33 | try |
34 | { |
35 | if (argc < 2) |
36 | { |
37 | std::cerr << "Usage: program n\n" ; |
38 | return 1; |
39 | } |
40 | |
41 | using T = UInt8; |
42 | |
43 | size_t n = atoi(argv[1]); |
44 | std::vector<T> data(n); |
45 | std::vector<T> data2(n); |
46 | |
47 | { |
48 | Stopwatch watch; |
49 | |
50 | for (size_t i = 0; i < n; ++i) |
51 | data[i] = lrand48();// / lrand48();// ^ (lrand48() << 24) ^ (lrand48() << 48); |
52 | |
53 | watch.stop(); |
54 | std::cerr << std::fixed << std::setprecision(2) |
55 | << "Generated " << n << " numbers (" << data.size() * sizeof(data[0]) / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., " |
56 | << data.size() * sizeof(data[0]) / watch.elapsedSeconds() / 1000000 << " MB/s." |
57 | << std::endl; |
58 | } |
59 | |
60 | std::vector<char> formatted; |
61 | formatted.reserve(n * 21); |
62 | |
63 | { |
64 | DB::WriteBufferFromVector wb(formatted); |
65 | // DB::CompressedWriteBuffer wb2(wb1); |
66 | // DB::AsynchronousWriteBuffer wb(wb2); |
67 | Stopwatch watch; |
68 | |
69 | UInt64 tsc = rdtsc(); |
70 | |
71 | for (size_t i = 0; i < n; ++i) |
72 | { |
73 | //writeIntTextTable(data[i], wb); |
74 | DB::writeIntText(data[i], wb); |
75 | //DB::writeIntText(data[i], wb); |
76 | DB::writeChar('\t', wb); |
77 | } |
78 | |
79 | tsc = rdtsc() - tsc; |
80 | |
81 | watch.stop(); |
82 | std::cerr << std::fixed << std::setprecision(2) |
83 | << "Written " << n << " numbers (" << wb.count() / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., " |
84 | << n / watch.elapsedSeconds() << " num/s., " |
85 | << wb.count() / watch.elapsedSeconds() / 1000000 << " MB/s., " |
86 | << watch.elapsed() / n << " ns/num., " |
87 | << tsc / n << " ticks/num., " |
88 | << watch.elapsed() / wb.count() << " ns/byte., " |
89 | << tsc / wb.count() << " ticks/byte." |
90 | << std::endl; |
91 | } |
92 | |
93 | { |
94 | DB::ReadBuffer rb(formatted.data(), formatted.size(), 0); |
95 | // DB::CompressedReadBuffer rb(rb_); |
96 | Stopwatch watch; |
97 | |
98 | for (size_t i = 0; i < n; ++i) |
99 | { |
100 | DB::readIntText(data2[i], rb); |
101 | DB::assertChar('\t', rb); |
102 | } |
103 | |
104 | watch.stop(); |
105 | std::cerr << std::fixed << std::setprecision(2) |
106 | << "Read " << n << " numbers (" << rb.count() / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., " |
107 | << rb.count() / watch.elapsedSeconds() / 1000000 << " MB/s." |
108 | << std::endl; |
109 | } |
110 | |
111 | std::cerr << (0 == memcmp(data.data(), data2.data(), data.size()) ? "Ok." : "Fail." ) << std::endl; |
112 | } |
113 | catch (const DB::Exception & e) |
114 | { |
115 | std::cerr << e.what() << ", " << e.displayText() << std::endl; |
116 | return 1; |
117 | } |
118 | |
119 | return 0; |
120 | } |
121 | |