fork download
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <chrono>
  8.  
  9. #define TOCOUT(output) \
  10.   if(!outputToCout) { \
  11.   buf = output##_t.rdbuf(); \
  12.   } else { \
  13.   buf = std::cout.rdbuf(); \
  14.   } \
  15.   std::ostream output(buf);
  16.  
  17. void fstreamBufferTest(){
  18.  
  19. const bool outputToCout = true;
  20.  
  21. const unsigned int multiplyStep = 1<<2;
  22. const unsigned int startLength = 1<<2;
  23. const unsigned int stopLength = 1<<24;
  24.  
  25. const unsigned int writeNTimes = 1; // Averaging over some many times!
  26. const unsigned int fileLength = 1<< 30; //104857600=100mb, 314572800=300mb , 1<< 30 =1GB
  27. std::string add = "1000.txt";
  28. unsigned int loops, restBytes;
  29.  
  30.  
  31. std::streambuf * buf;
  32.  
  33. std::ofstream output1_t("FStreamTest-FstreamBuffering-OwnBufferSet-"+add);
  34. TOCOUT(output1);
  35. output1 << "#Buffer Length \tTimeToWrite \tWriteSpeed [mb/s]" << std::endl;
  36.  
  37. std::ofstream output2_t("FStreamTest-ManualBuffering-StdStreamBuffer-"+add);
  38. TOCOUT(output2);
  39. output2 << "#Buffer Length \tTimeToWrite \tWriteSpeed [mb/s]" << std::endl;
  40.  
  41. std::ofstream output3_t("FStreamTest-ManualBuffering-NoInternalStreamBuffer-"+add);
  42. TOCOUT(output3);
  43. output3 << "#Buffer Length \tTimeToWrite \tWriteSpeed [mb/s]" << std::endl;
  44.  
  45. std::ofstream output4_t("FStreamTest-NoManualBuffering-NoInternalStreamBuffer-"+add);
  46. TOCOUT(output4);
  47. output4 << "#Buffer Length \tTimeToWrite\tWriteSpeed [mb/s]" << std::endl;
  48.  
  49. std::ofstream output5_t("FStreamTest-NoManualBuffering-StdStreamBuffer-"+add);
  50. TOCOUT(output5);
  51. output5 << "#Buffer Length \tTimeToWrite \tWriteSpeed [mb/s]" << std::endl;
  52.  
  53. // To Cout
  54.  
  55.  
  56. typedef std::chrono::duration<double> fsec;
  57. typedef std::chrono::high_resolution_clock Clock;
  58.  
  59.  
  60.  
  61. // Test Data for the Buffer
  62. bool removeFile = true;
  63. char value = 1;
  64. char *testData = new char[fileLength]; // Just Garbage 1GB!!
  65. std::memset(testData,value,fileLength);
  66.  
  67. // Preallocate file;
  68. if(!removeFile){
  69. std::fstream stream;
  70. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  71. for(int i = 0; i < writeNTimes; i++){
  72. stream.write(testData, fileLength );
  73. }
  74. stream.close();
  75. }else{
  76. if( remove( "test.dat" ) == 0){
  77. std::cout << "File deleted at start!" << std::endl;
  78. }
  79. }
  80.  
  81. for(unsigned int bufL = startLength; bufL <= stopLength; bufL = bufL * multiplyStep){
  82.  
  83. // First Test with Fstream Buffering!
  84. {
  85. std::cout << "Doing test: FStream Buffering: " << bufL <<std::endl;
  86. char * buffer = new char[bufL];
  87. //open Stream
  88. std::fstream stream;
  89. stream.rdbuf()->pubsetbuf(buffer, bufL);
  90. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  91.  
  92. // Write whole 1gb file! we have fstream buffering the stuff
  93. auto t1 = Clock::now();
  94. for(int i = 0; i < writeNTimes; i++){
  95. stream.write(testData, fileLength );
  96. }
  97. stream.close();
  98. auto t2 = Clock::now();
  99.  
  100. //Calculate timing
  101. fsec time = (t2 - t1) / writeNTimes;
  102. output1 << bufL << "\t" << time.count() <<"\t" << (fileLength/time.count()) / (1024*1024) << std::endl;
  103.  
  104. delete buffer;
  105. if(removeFile){
  106. if( remove( "test.dat" ) != 0){
  107. std::cerr << "File not deleted" << std::endl;
  108. };
  109. }
  110. }
  111.  
  112. // Second Test with Manual Buffering!
  113. {
  114. std::cout << "Doing test: Manual Buffering: " << bufL <<std::endl;
  115. // Calculate the loops to write fileLength
  116. loops = fileLength / bufL;
  117. restBytes = fileLength % bufL;
  118.  
  119. //open Stream
  120. std::fstream stream;
  121. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  122. // TODO stream buf -> 0
  123.  
  124. // Write 1GB File in loops of bufL
  125. auto t1 = Clock::now();
  126. for(int i = 0; i < writeNTimes; i++){
  127. for(int i = 0; i < loops; i++){
  128. stream.write(testData, bufL );
  129. }
  130. stream.write(testData, restBytes );
  131. }
  132. stream.close();
  133. auto t2 = Clock::now();
  134.  
  135. //Calculate timing
  136. fsec time = (t2 - t1) / writeNTimes;
  137. output2 << bufL << "\t" << time.count() <<"\t" << (fileLength/time.count()) / (1024*1024) << std::endl;
  138. if(removeFile){
  139. if( remove( "test.dat" ) != 0){
  140. std::cerr << "File not deleted" << std::endl;
  141. };
  142. }
  143. }
  144.  
  145. // Second Test with Manual Buffering!
  146. {
  147. std::cout << "Doing test: Manual Buffering (no internal stream buffer): " << bufL <<std::endl;
  148. // Calculate the loops to write fileLength
  149. loops = fileLength / bufL;
  150. restBytes = fileLength % bufL;
  151.  
  152. //open Stream
  153. std::fstream stream;
  154. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  155. stream.rdbuf()->pubsetbuf(0, 0);
  156.  
  157. // Write 1GB File in loops of bufL
  158. auto t1 = Clock::now();
  159. for(int i = 0; i < writeNTimes; i++){
  160. for(int i = 0; i < loops; i++){
  161. stream.write(testData, bufL );
  162. }
  163. stream.write(testData, restBytes );
  164. }
  165. stream.close();
  166. auto t2 = Clock::now();
  167.  
  168. //Calculate timing
  169. fsec time = (t2 - t1) / writeNTimes;
  170. output3 << bufL << "\t" << time.count() <<"\t" << (fileLength/time.count()) / (1024*1024) << std::endl;
  171. if(removeFile){
  172. if( remove( "test.dat" ) != 0){
  173. std::cerr << "File not deleted" << std::endl;
  174. };
  175. }
  176. }
  177.  
  178.  
  179. {
  180. std::cout << "Doing test: No manual Buffering (no internal stream buffer): " << bufL <<std::endl;
  181. // Calculate the loops to write fileLength
  182. loops = fileLength / bufL;
  183. restBytes = fileLength % bufL;
  184.  
  185. //open Stream
  186. std::fstream stream;
  187. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  188. stream.rdbuf()->pubsetbuf(0, 0);
  189.  
  190. // Write 1GB File in loops of bufL
  191. auto t1 = Clock::now();
  192. for(int i = 0; i < writeNTimes; i++){
  193. stream.write(testData, fileLength );
  194. }
  195. stream.close();
  196. auto t2 = Clock::now();
  197.  
  198. //Calculate timing
  199. fsec time = (t2 - t1) / writeNTimes;
  200. output4 << bufL << "\t" << time.count() <<"\t" << (fileLength/time.count()) / (1024*1024) << std::endl;
  201. if(removeFile){
  202. if( remove( "test.dat" ) != 0){
  203. std::cerr << "File not deleted" << std::endl;
  204. };
  205. }
  206. }
  207.  
  208. {
  209. std::cout << "Doing test: No manual Buffering (std stream buffer): " << bufL <<std::endl;
  210. //Calculate the loops to write fileLength
  211. loops = fileLength / bufL;
  212. restBytes = fileLength % bufL;
  213.  
  214. //open Stream
  215. std::fstream stream;
  216. stream.open("test.dat", std::ios::binary | std::ios::trunc | std::ios::out);
  217.  
  218. // Write 1GB File in loops of bufL
  219. auto t1 = Clock::now();
  220. for(int i = 0; i < writeNTimes; i++){
  221. stream.write(testData, fileLength );
  222. }
  223. stream.close();
  224. auto t2 = Clock::now();
  225.  
  226. //Calculate timing
  227. fsec time = (t2 - t1)/ writeNTimes;
  228. output5 << bufL << "\t" << time.count() <<"\t" << (fileLength/time.count()) / (1024*1024) << std::endl;
  229. if(removeFile){
  230. if( remove( "test.dat" ) != 0){
  231. std::cerr << "File not deleted" << std::endl;
  232. };
  233. }
  234. }
  235.  
  236.  
  237.  
  238. }
  239.  
  240.  
  241.  
  242. }
  243.  
  244. int main() {
  245. fstreamBufferTest();
  246. }
Runtime error #stdin #stdout #stderr 0s 4512KB
stdin
Standard input is empty
stdout
#Buffer Length 	TimeToWrite 	WriteSpeed [mb/s]
#Buffer Length 	TimeToWrite 	WriteSpeed [mb/s]
#Buffer Length 	TimeToWrite 	WriteSpeed [mb/s]
#Buffer Length 	TimeToWrite	WriteSpeed [mb/s]
#Buffer Length 	TimeToWrite 	WriteSpeed [mb/s]
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc