Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

214 rindas
5.3KB

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