vakho

Fast Reading

Mar 31st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>                              // for std::find
  4. #include <iostream>                               // for std::cout
  5. #include <cstring>
  6. #include <vector>
  7.  
  8. #include <boost/iostreams/device/mapped_file.hpp> // for mmap
  9. #include <boost/iostreams/stream.hpp>             // for stream
  10. #include <boost/algorithm/string/split.hpp>       // for split operation
  11. #include<boost/algorithm/string.hpp>              // for is_any_of
  12.  
  13. #include <chrono>
  14.  
  15. using hrclock = std::chrono::high_resolution_clock;
  16. using namespace std;
  17.  
  18. const string file_path = "bcsstk01.txt";
  19.  
  20. int main()
  21. {
  22.     vector<double> vec;
  23.  
  24.     int n(48), nnz(224);
  25.     long diffs[4];
  26.  
  27.     // Basic reading
  28.     ifstream ifs2(file_path);
  29.     int i, j;
  30.     double v;
  31.     clock_t start = clock();
  32.     while (ifs2 >> i >> j >> v) {
  33.         /* action */
  34.         cout << i << "\t" << j << "\t" << v << "\n";
  35.         vec.push_back(v);
  36.     }
  37.     vec.clear();
  38.     clock_t end = clock();
  39.     long diff = end - start;
  40.     diffs[0] = diff;
  41.     cout << "Basic: " << diff << endl;
  42.     ifs2.close();
  43.  
  44.     // Getline reading
  45.     ifstream ifs(file_path);
  46.     std::string s;
  47.     start = clock();
  48.     while (getline(ifs, s)) {
  49.         auto it = s.begin();
  50.         if (it != s.end()) {
  51.             string ss = string{ it, s.end() };
  52.             /* action */
  53.             cout << ss << "\n";
  54.             vector<string> strVec;
  55.             boost::algorithm::split(strVec, ss, boost::is_any_of("\t "), boost::token_compress_on);
  56.             vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
  57.         }
  58.     }
  59.     vec.clear();
  60.     end = clock();
  61.     ifs.close();
  62.     diff = end - start;
  63.     diffs[1] = diff;
  64.     cout << "Getline: " << diff << endl;
  65.    
  66.     /*
  67.         http://stackoverflow.com/questions/26252893/read-till-end-of-a-boost-memory-mapped-file-in-vc
  68.     */
  69.     // Boost (Using the raw Device source)
  70.     boost::iostreams::mapped_file mmap(file_path, boost::iostreams::mapped_file::readonly);
  71.     auto f = mmap.const_data();
  72.     auto l = f + mmap.size();
  73.  
  74.     uintmax_t m_numLines = 0;
  75.     start = clock();
  76.     int k = 0;
  77.     while (f && f != l) {
  78.         if ((f = static_cast<const char*>(memchr(f, '\n', l - f)))) {
  79.             cout << f << "\n";
  80.             vector<string> strVec;
  81.             boost::algorithm::split(strVec, f, boost::is_any_of("\t "), boost::token_compress_on);
  82.             vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
  83.  
  84.             m_numLines++, f++;
  85.         }
  86.     }
  87.     vec.clear();
  88.     end = clock();
  89.     diff = end - start;
  90.     diffs[2] = diff;
  91.     cout << "Boost2: " << diff << "\n";
  92.     cout << "m_numLines = " << m_numLines << "\n";
  93.  
  94.     // Boost (Wrapping the source device in a istream2)
  95.     using boost::iostreams::mapped_file_source;
  96.     using boost::iostreams::stream;
  97.  
  98.     mapped_file_source file(file_path);
  99.     stream<mapped_file_source> is(file, std::ios::binary);
  100.  
  101.     std::string line;
  102.  
  103.     m_numLines = 0;
  104.     start = clock();
  105.     while (std::getline(is, line))
  106.     {
  107.         cout << line << "\n";
  108.         vector<string> strVec;
  109.         boost::algorithm::split(strVec, line, boost::is_any_of("\t "), boost::token_compress_on);
  110.         vec.push_back(atof(strVec[strVec.size() - 1].c_str()));
  111.  
  112.         m_numLines++;
  113.     }
  114.     vec.clear();
  115.     end = clock();
  116.     diff = end - start;
  117.     diffs[3] = diff;
  118.     cout << "Boost1 (istream): " << diff << "\n";
  119.     cout << "m_numLines = " << m_numLines << "\n";
  120.  
  121.     for (int l = 0; l < 4; l++) {
  122.         cout << l << "\t" << diffs[l] << "\n";
  123.     }
  124. }
  125.  
  126. // FILE
  127. 1 1 2832268.51852
  128. 5 1 1e6
  129. 6 1 2083333.33333
  130. 7 1 -3333.33333333
  131. 11 1 1e6
  132. 19 1 -2.8e6
  133. 25 1 -28935.1851852
  134. 30 1 2083333.33333
  135. 2 2 1635447.53086
  136. 4 2 -2e6
  137. 6 2 5555555.55555
  138. 8 2 -6666.66666667
  139. 10 2 -2e6
  140. 20 2 -30864.1975309
  141. 24 2 5555555.55555
  142. 26 2 -1597916.66667
  143. 3 3 1724367.28395
  144. 4 3 -2083333.33333
  145. 5 3 -2777777.77778
  146. 9 3 -1.68e6
  147. 21 3 -15432.0987654
  148. 23 3 -2777777.77778
  149. 27 3 -28935.1851852
  150. 28 3 -2083333.33333
  151. 4 4 1003333333.33
  152. 8 4 2e6
  153. 10 4 4e8
  154. 22 4 -3333333.33333
  155. 27 4 2083333.33333
  156. 28 4 1e8
  157. 5 5 1.0675e9
  158. 7 5 -1e6
  159. 11 5 2e8
  160. 21 5 2777777.77778
  161. 23 5 333333333.333
  162. 29 5 -833333.333333
  163. 6 6 1535333333.33
  164. 12 6 -2e6
  165. 20 6 -5555555.55555
  166. 24 6 666666666.667
  167. 25 6 -2083333.33333
  168. 30 6 1e8
  169. 7 7 2832268.51852
  170. 11 7 -1e6
  171. 12 7 2083333.33333
  172. 13 7 -2.8e6
  173. 31 7 -28935.1851852
  174. 36 7 2083333.33333
  175. 8 8 1635447.53086
  176. 10 8 2e6
  177. 12 8 5555555.55555
  178. 14 8 -30864.1975309
  179. 18 8 5555555.55555
  180. 32 8 -1597916.66667
  181. 9 9 1724367.28395
  182. 10 9 -2083333.33333
  183. 11 9 -2777777.77778
  184. 15 9 -15432.0987654
  185. 17 9 -2777777.77778
  186. 33 9 -28935.1851852
  187. 34 9 -2083333.33333
  188. 10 10 1003333333.33
  189. 16 10 -3333333.33333
  190. 33 10 2083333.33333
  191. 34 10 1e8
  192. 11 11 1.0675e9
  193. 15 11 2777777.77778
  194. 17 11 333333333.333
  195. 35 11 -833333.333333
  196. 12 12 1535333333.33
  197. 14 12 -5555555.55555
  198. 18 12 666666666.667
  199. 31 12 -2083333.33333
  200. 36 12 1e8
  201. 13 13 2836099.4695
  202. 17 13 -2149285.29451
  203. 18 13 2359161.80402
  204. 19 13 -3333.33333333
  205. 23 13 -1e6
  206. 37 13 -28935.1851852
  207. 42 13 2083333.33333
  208. 43 13 -3830.95098171
  209. 47 13 -1149285.29451
  210. 48 13 275828.470683
  211. 14 14 1767410.74446
  212. 15 14 517922.131816
  213. 16 14 4298570.58902
  214. 18 14 -5555555.55555
  215. 20 14 -6666.66666667
  216. 22 14 2e6
  217. 38 14 -1597916.66667
  218. 44 14 -131963.213599
  219. 45 14 -517922.131816
  220. 46 14 2298570.58902
  221. 15 15 3890038.06848
  222. 16 15 -2634990.2747
  223. 17 15 2777777.77778
  224. 21 15 -1.68e6
  225. 39 15 -28935.1851852
  226. 40 15 -2083333.33333
  227. 44 15 -517922.131816
  228. 45 15 -2165670.78453
  229. 46 15 -551656.941367
  230. 16 16 1975720635.31
  231. 20 16 -2e6
  232. 22 16 4e8
  233. 39 16 2083333.33333
  234. 40 16 1e8
  235. 44 16 -2298570.58902
  236. 45 16 551656.941366
  237. 46 16 486193650.99
  238. 17 17 1527346515.47
  239. 18 17 -109779731.332
  240. 19 17 1e6
  241. 23 17 2e8
  242. 41 17 -833333.333333
  243. 43 17 1149285.29451
  244. 47 17 229724661.236
  245. 48 17 -55717351.0779
  246. 18 18 1564111437.11
  247. 24 18 -2e6
  248. 37 18 -2083333.33333
  249. 42 18 1e8
  250. 43 18 -275828.470683
  251. 47 18 -55717351.0779
  252. 48 18 10941196.0038
  253. 19 19 2832268.51852
  254. 23 19 1e6
  255. 24 19 2083333.33333
  256. 43 19 -28935.1851852
  257. 48 19 2083333.33333
  258. 20 20 1635447.53086
  259. 22 20 -2e6
  260. 24 20 -5555555.55555
  261. 44 20 -1597916.66667
  262. 21 21 1724367.28395
  263. 22 21 -2083333.33333
  264. 23 21 2777777.77778
  265. 45 21 -28935.1851852
  266. 46 21 -2083333.33333
  267. 22 22 1003333333.33
  268. 45 22 2083333.33333
  269. 46 22 1e8
  270. 23 23 1.0675e9
  271. 47 23 -833333.333333
  272. 24 24 1535333333.33
  273. 43 24 -2083333.33333
  274. 48 24 1e8
  275. 25 25 60879.6296296
  276. 29 25 1.25e6
  277. 30 25 416666.666667
  278. 31 25 -4166.66666667
  279. 35 25 1.25e6
  280. 26 26 3372916.66667
  281. 28 26 -2.5e6
  282. 32 26 -8333.33333333
  283. 34 26 -2.5e6
  284. 27 27 2411712.96296
  285. 28 27 -416666.666667
  286. 33 27 -2.355e6
  287. 28 28 1.5e9
  288. 32 28 2.5e6
  289. 34 28 5e8
  290. 29 29 501833333.333
  291. 31 29 -1.25e6
  292. 35 29 2.5e8
  293. 30 30 5.025e8
  294. 36 30 -2.5e6
  295. 31 31 3985879.62963
  296. 35 31 -1.25e6
  297. 36 31 416666.666667
  298. 37 31 -3.925e6
  299. 32 32 3411496.91358
  300. 34 32 2.5e6
  301. 36 32 6944444.44444
  302. 38 32 -38580.2469136
  303. 42 32 6944444.44445
  304. 33 33 2431003.08642
  305. 34 33 -416666.666667
  306. 35 33 -3472222.22222
  307. 39 33 -19290.1234568
  308. 41 33 -3472222.22222
  309. 34 34 1504166666.67
  310. 40 34 -4166666.66667
  311. 35 35 1335166666.67
  312. 39 35 3472222.22222
  313. 41 35 416666666.667
  314. 36 36 2169166666.67
  315. 38 36 -6944444.44444
  316. 42 36 833333333.333
  317. 37 37 3985879.62963
  318. 41 37 -1.25e6
  319. 42 37 416666.666667
  320. 43 37 -4166.66666667
  321. 47 37 -1.25e6
  322. 38 38 3411496.91358
  323. 40 38 2.5e6
  324. 42 38 -6944444.44445
  325. 44 38 -8333.33333333
  326. 46 38 2.5e6
  327. 39 39 2431003.08642
  328. 40 39 -416666.666667
  329. 41 39 3472222.22222
  330. 45 39 -2.355e6
  331. 40 40 1504166666.67
  332. 44 40 -2.5e6
  333. 46 40 5e8
  334. 41 41 1335166666.67
  335. 43 41 1.25e6
  336. 47 41 2.5e8
  337. 42 42 2169166666.67
  338. 48 42 -2.5e6
  339. 43 43 64710.5806113
  340. 47 43 2399285.29451
  341. 48 43 140838.195984
  342. 44 44 3504879.88027
  343. 45 44 517922.131816
  344. 46 44 -4798570.58902
  345. 45 45 4577383.74749
  346. 46 45 134990.2747
  347. 46 46 2472387301.98
  348. 47 47 961679848.804
  349. 48 47 -109779731.332
  350. 48 48 531278103.775
Advertisement
Add Comment
Please, Sign In to add comment