選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

215 行
5.3KB

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