Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm> // for std::find
- #include <iostream> // for std::cout
- #include <cstring>
- #include <vector>
- #include <boost/iostreams/device/mapped_file.hpp> // for mmap
- #include <boost/iostreams/stream.hpp> // for stream
- #include <boost/algorithm/string/split.hpp> // for split operation
- #include<boost/algorithm/string.hpp> // for is_any_of
- #include <chrono>
- using hrclock = std::chrono::high_resolution_clock;
- using namespace std;
- const string file_path = "bcsstk01.txt";
- int main()
- {
- vector<double> vec;
- int n(48), nnz(224);
- long diffs[4];
- // Basic reading
- ifstream ifs2(file_path);
- int i, j;
- double v;
- clock_t start = clock();
- while (ifs2 >> i >> j >> v) {
- /* action */
- cout << i << "\t" << j << "\t" << v << "\n";
- vec.push_back(v);
- }
- vec.clear();
- clock_t end = clock();
- long diff = end - start;
- diffs[0] = diff;
- cout << "Basic: " << diff << endl;
- ifs2.close();
- // Getline reading
- ifstream ifs(file_path);
- std::string s;
- start = clock();
- while (getline(ifs, s)) {
- auto it = s.begin();
- if (it != s.end()) {
- string ss = string{ it, s.end() };
- /* action */
- cout << ss << "\n";
- vector<string> strVec;
- boost::algorithm::split(strVec, ss, boost::is_any_of("\t "), boost::token_compress_on);
- vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
- }
- }
- vec.clear();
- end = clock();
- ifs.close();
- diff = end - start;
- diffs[1] = diff;
- cout << "Getline: " << diff << endl;
- /*
- http://stackoverflow.com/questions/26252893/read-till-end-of-a-boost-memory-mapped-file-in-vc
- */
- // Boost (Using the raw Device source)
- boost::iostreams::mapped_file mmap(file_path, boost::iostreams::mapped_file::readonly);
- auto f = mmap.const_data();
- auto l = f + mmap.size();
- uintmax_t m_numLines = 0;
- start = clock();
- int k = 0;
- while (f && f != l) {
- if ((f = static_cast<const char*>(memchr(f, '\n', l - f)))) {
- cout << f << "\n";
- vector<string> strVec;
- boost::algorithm::split(strVec, f, boost::is_any_of("\t "), boost::token_compress_on);
- vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
- m_numLines++, f++;
- }
- }
- vec.clear();
- end = clock();
- diff = end - start;
- diffs[2] = diff;
- cout << "Boost2: " << diff << "\n";
- cout << "m_numLines = " << m_numLines << "\n";
- // Boost (Wrapping the source device in a istream2)
- using boost::iostreams::mapped_file_source;
- using boost::iostreams::stream;
- mapped_file_source file(file_path);
- stream<mapped_file_source> is(file, std::ios::binary);
- std::string line;
- m_numLines = 0;
- start = clock();
- while (std::getline(is, line))
- {
- cout << line << "\n";
- vector<string> strVec;
- boost::algorithm::split(strVec, line, boost::is_any_of("\t "), boost::token_compress_on);
- vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
- m_numLines++;
- }
- vec.clear();
- end = clock();
- diff = end - start;
- diffs[3] = diff;
- cout << "Boost1 (istream): " << diff << "\n";
- cout << "m_numLines = " << m_numLines << "\n";
- for (int l = 0; l < 4; l++) {
- cout << l << "\t" << diffs[l] << "\n";
- }
- }
- // FILE
- 1 1 2832268.51852
- 5 1 1e6
- 6 1 2083333.33333
- 7 1 -3333.33333333
- 11 1 1e6
- 19 1 -2.8e6
- 25 1 -28935.1851852
- 30 1 2083333.33333
- 2 2 1635447.53086
- 4 2 -2e6
- 6 2 5555555.55555
- 8 2 -6666.66666667
- 10 2 -2e6
- 20 2 -30864.1975309
- 24 2 5555555.55555
- 26 2 -1597916.66667
- 3 3 1724367.28395
- 4 3 -2083333.33333
- 5 3 -2777777.77778
- 9 3 -1.68e6
- 21 3 -15432.0987654
- 23 3 -2777777.77778
- 27 3 -28935.1851852
- 28 3 -2083333.33333
- 4 4 1003333333.33
- 8 4 2e6
- 10 4 4e8
- 22 4 -3333333.33333
- 27 4 2083333.33333
- 28 4 1e8
- 5 5 1.0675e9
- 7 5 -1e6
- 11 5 2e8
- 21 5 2777777.77778
- 23 5 333333333.333
- 29 5 -833333.333333
- 6 6 1535333333.33
- 12 6 -2e6
- 20 6 -5555555.55555
- 24 6 666666666.667
- 25 6 -2083333.33333
- 30 6 1e8
- 7 7 2832268.51852
- 11 7 -1e6
- 12 7 2083333.33333
- 13 7 -2.8e6
- 31 7 -28935.1851852
- 36 7 2083333.33333
- 8 8 1635447.53086
- 10 8 2e6
- 12 8 5555555.55555
- 14 8 -30864.1975309
- 18 8 5555555.55555
- 32 8 -1597916.66667
- 9 9 1724367.28395
- 10 9 -2083333.33333
- 11 9 -2777777.77778
- 15 9 -15432.0987654
- 17 9 -2777777.77778
- 33 9 -28935.1851852
- 34 9 -2083333.33333
- 10 10 1003333333.33
- 16 10 -3333333.33333
- 33 10 2083333.33333
- 34 10 1e8
- 11 11 1.0675e9
- 15 11 2777777.77778
- 17 11 333333333.333
- 35 11 -833333.333333
- 12 12 1535333333.33
- 14 12 -5555555.55555
- 18 12 666666666.667
- 31 12 -2083333.33333
- 36 12 1e8
- 13 13 2836099.4695
- 17 13 -2149285.29451
- 18 13 2359161.80402
- 19 13 -3333.33333333
- 23 13 -1e6
- 37 13 -28935.1851852
- 42 13 2083333.33333
- 43 13 -3830.95098171
- 47 13 -1149285.29451
- 48 13 275828.470683
- 14 14 1767410.74446
- 15 14 517922.131816
- 16 14 4298570.58902
- 18 14 -5555555.55555
- 20 14 -6666.66666667
- 22 14 2e6
- 38 14 -1597916.66667
- 44 14 -131963.213599
- 45 14 -517922.131816
- 46 14 2298570.58902
- 15 15 3890038.06848
- 16 15 -2634990.2747
- 17 15 2777777.77778
- 21 15 -1.68e6
- 39 15 -28935.1851852
- 40 15 -2083333.33333
- 44 15 -517922.131816
- 45 15 -2165670.78453
- 46 15 -551656.941367
- 16 16 1975720635.31
- 20 16 -2e6
- 22 16 4e8
- 39 16 2083333.33333
- 40 16 1e8
- 44 16 -2298570.58902
- 45 16 551656.941366
- 46 16 486193650.99
- 17 17 1527346515.47
- 18 17 -109779731.332
- 19 17 1e6
- 23 17 2e8
- 41 17 -833333.333333
- 43 17 1149285.29451
- 47 17 229724661.236
- 48 17 -55717351.0779
- 18 18 1564111437.11
- 24 18 -2e6
- 37 18 -2083333.33333
- 42 18 1e8
- 43 18 -275828.470683
- 47 18 -55717351.0779
- 48 18 10941196.0038
- 19 19 2832268.51852
- 23 19 1e6
- 24 19 2083333.33333
- 43 19 -28935.1851852
- 48 19 2083333.33333
- 20 20 1635447.53086
- 22 20 -2e6
- 24 20 -5555555.55555
- 44 20 -1597916.66667
- 21 21 1724367.28395
- 22 21 -2083333.33333
- 23 21 2777777.77778
- 45 21 -28935.1851852
- 46 21 -2083333.33333
- 22 22 1003333333.33
- 45 22 2083333.33333
- 46 22 1e8
- 23 23 1.0675e9
- 47 23 -833333.333333
- 24 24 1535333333.33
- 43 24 -2083333.33333
- 48 24 1e8
- 25 25 60879.6296296
- 29 25 1.25e6
- 30 25 416666.666667
- 31 25 -4166.66666667
- 35 25 1.25e6
- 26 26 3372916.66667
- 28 26 -2.5e6
- 32 26 -8333.33333333
- 34 26 -2.5e6
- 27 27 2411712.96296
- 28 27 -416666.666667
- 33 27 -2.355e6
- 28 28 1.5e9
- 32 28 2.5e6
- 34 28 5e8
- 29 29 501833333.333
- 31 29 -1.25e6
- 35 29 2.5e8
- 30 30 5.025e8
- 36 30 -2.5e6
- 31 31 3985879.62963
- 35 31 -1.25e6
- 36 31 416666.666667
- 37 31 -3.925e6
- 32 32 3411496.91358
- 34 32 2.5e6
- 36 32 6944444.44444
- 38 32 -38580.2469136
- 42 32 6944444.44445
- 33 33 2431003.08642
- 34 33 -416666.666667
- 35 33 -3472222.22222
- 39 33 -19290.1234568
- 41 33 -3472222.22222
- 34 34 1504166666.67
- 40 34 -4166666.66667
- 35 35 1335166666.67
- 39 35 3472222.22222
- 41 35 416666666.667
- 36 36 2169166666.67
- 38 36 -6944444.44444
- 42 36 833333333.333
- 37 37 3985879.62963
- 41 37 -1.25e6
- 42 37 416666.666667
- 43 37 -4166.66666667
- 47 37 -1.25e6
- 38 38 3411496.91358
- 40 38 2.5e6
- 42 38 -6944444.44445
- 44 38 -8333.33333333
- 46 38 2.5e6
- 39 39 2431003.08642
- 40 39 -416666.666667
- 41 39 3472222.22222
- 45 39 -2.355e6
- 40 40 1504166666.67
- 44 40 -2.5e6
- 46 40 5e8
- 41 41 1335166666.67
- 43 41 1.25e6
- 47 41 2.5e8
- 42 42 2169166666.67
- 48 42 -2.5e6
- 43 43 64710.5806113
- 47 43 2399285.29451
- 48 43 140838.195984
- 44 44 3504879.88027
- 45 44 517922.131816
- 46 44 -4798570.58902
- 45 45 4577383.74749
- 46 45 134990.2747
- 46 46 2472387301.98
- 47 47 961679848.804
- 48 47 -109779731.332
- 48 48 531278103.775
Advertisement
Add Comment
Please, Sign In to add comment