You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.1KB

  1. // Benchmark comparing SdFile and StdioStream.
  2. #include <SPI.h>
  3. #include "SdFat.h"
  4. // Define PRINT_FIELD nonzero to use printField.
  5. #define PRINT_FIELD 0
  6. // Number of lines to list on Serial.
  7. #define STDIO_LIST_COUNT 0
  8. #define VERIFY_CONTENT 0
  9. const uint8_t SD_CS_PIN = SS;
  10. SdFat sd;
  11. SdFile printFile;
  12. StdioStream stdioFile;
  13. float f[100];
  14. char buf[20];
  15. char* label[] =
  16. { "uint8_t 0 to 255, 100 times ", "uint16_t 0 to 20000",
  17. "uint32_t 0 to 20000", "uint32_t 1000000000 to 1000010000",
  18. "float nnn.ffff, 10000 times"
  19. };
  20. //------------------------------------------------------------------------------
  21. void setup() {
  22. uint32_t m;
  23. uint32_t printSize;
  24. uint32_t stdioSize;
  25. uint32_t printTime;
  26. uint32_t stdioTime;
  27. Serial.begin(9600);
  28. while (!Serial) {}
  29. Serial.println(F("Type any character to start"));
  30. while (!Serial.available());
  31. Serial.println(F("Starting test"));
  32. if (!sd.begin(SD_CS_PIN)) {
  33. sd.errorHalt();
  34. }
  35. for (uint8_t i = 0; i < 100; i++) {
  36. f[i] = 123.0 + 0.1234*i;
  37. }
  38. for (uint8_t dataType = 0; dataType < 5; dataType++) {
  39. for (uint8_t fileType = 0; fileType < 2; fileType++) {
  40. if (!fileType) {
  41. if (!printFile.open("print.txt", O_CREAT | O_RDWR | O_TRUNC)) {
  42. Serial.println(F("open fail"));
  43. return;
  44. }
  45. printTime = millis();
  46. switch (dataType) {
  47. case 0:
  48. for (uint16_t i =0; i < 100; i++) {
  49. for (uint8_t j = 0; j < 255; j++) {
  50. printFile.println(j);
  51. }
  52. }
  53. break;
  54. case 1:
  55. for (uint16_t i = 0; i < 20000; i++) {
  56. printFile.println(i);
  57. }
  58. break;
  59. case 2:
  60. for (uint32_t i = 0; i < 20000; i++) {
  61. printFile.println(i);
  62. }
  63. break;
  64. case 3:
  65. for (uint16_t i = 0; i < 10000; i++) {
  66. printFile.println(i + 1000000000UL);
  67. }
  68. break;
  69. case 4:
  70. for (int j = 0; j < 100; j++) {
  71. for (uint8_t i = 0; i < 100; i++) {
  72. printFile.println(f[i], 4);
  73. }
  74. }
  75. break;
  76. default:
  77. break;
  78. }
  79. printFile.sync();
  80. printTime = millis() - printTime;
  81. printFile.rewind();
  82. printSize = printFile.fileSize();
  83. } else {
  84. if (!stdioFile.fopen("stream.txt", "w+")) {
  85. Serial.println(F("fopen fail"));
  86. return;
  87. }
  88. stdioTime = millis();
  89. switch (dataType) {
  90. case 0:
  91. for (uint16_t i =0; i < 100; i++) {
  92. for (uint8_t j = 0; j < 255; j++) {
  93. #if PRINT_FIELD
  94. stdioFile.printField(j, '\n');
  95. #else // PRINT_FIELD
  96. stdioFile.println(j);
  97. #endif // PRINT_FIELD
  98. }
  99. }
  100. break;
  101. case 1:
  102. for (uint16_t i = 0; i < 20000; i++) {
  103. #if PRINT_FIELD
  104. stdioFile.printField(i, '\n');
  105. #else // PRINT_FIELD
  106. stdioFile.println(i);
  107. #endif // PRINT_FIELD
  108. }
  109. break;
  110. case 2:
  111. for (uint32_t i = 0; i < 20000; i++) {
  112. #if PRINT_FIELD
  113. stdioFile.printField(i, '\n');
  114. #else // PRINT_FIELD
  115. stdioFile.println(i);
  116. #endif // PRINT_FIELD
  117. }
  118. break;
  119. case 3:
  120. for (uint16_t i = 0; i < 10000; i++) {
  121. #if PRINT_FIELD
  122. stdioFile.printField(i + 1000000000UL, '\n');
  123. #else // PRINT_FIELD
  124. stdioFile.println(i + 1000000000UL);
  125. #endif // PRINT_FIELD
  126. }
  127. break;
  128. case 4:
  129. for (int j = 0; j < 100; j++) {
  130. for (uint8_t i = 0; i < 100; i++) {
  131. #if PRINT_FIELD
  132. stdioFile.printField(f[i], '\n', 4);
  133. #else // PRINT_FIELD
  134. stdioFile.println(f[i], 4);
  135. #endif // PRINT_FIELD
  136. }
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142. stdioFile.fflush();
  143. stdioTime = millis() - stdioTime;
  144. stdioSize = stdioFile.ftell();
  145. if (STDIO_LIST_COUNT) {
  146. size_t len;
  147. stdioFile.rewind();
  148. for (int i = 0; i < STDIO_LIST_COUNT; i++) {
  149. stdioFile.fgets(buf, sizeof(buf), &len);
  150. Serial.print(len);
  151. Serial.print(',');
  152. Serial.print(buf);
  153. }
  154. }
  155. }
  156. }
  157. Serial.println(label[dataType]);
  158. if (VERIFY_CONTENT && printSize == stdioSize) {
  159. printFile.rewind();
  160. stdioFile.rewind();
  161. for (uint32_t i = 0; i < stdioSize; i++) {
  162. if (printFile.read() != stdioFile.getc()) {
  163. Serial.print(F("Files differ at pos: "));
  164. Serial.println(i);
  165. return;
  166. }
  167. }
  168. }
  169. Serial.print(F("fileSize: "));
  170. if (printSize != stdioSize) {
  171. Serial.print(printSize);
  172. Serial.print(F(" != "));
  173. }
  174. Serial.println(stdioSize);
  175. Serial.print(F("print millis: "));
  176. Serial.println(printTime);
  177. Serial.print(F("stdio millis: "));
  178. Serial.println(stdioTime);
  179. Serial.print(F("ratio: "));
  180. Serial.println((float)printTime/(float)stdioTime);
  181. Serial.println();
  182. printFile.close();
  183. stdioFile.fclose();
  184. }
  185. Serial.println(F("Done"));
  186. }
  187. void loop() {}