speed.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * speed.c:
  3. * Simple program to measure the speed of the various GPIO
  4. * access mechanisms.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <wiringPi.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #define FAST_COUNT 10000000
  30. #define SLOW_COUNT 1000000
  31. int main (void)
  32. {
  33. int i ;
  34. uint32_t start, end, count, sum, perSec ;
  35. printf ("Raspberry Pi wiringPi speed test program\n") ;
  36. // Start the standard way
  37. if (wiringPiSetup () == -1)
  38. exit (1) ;
  39. printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
  40. pinMode (0, OUTPUT) ;
  41. sum = 0 ;
  42. for (i = 0 ; i < 3 ; ++i)
  43. {
  44. printf (" Pass: %d: ", i) ;
  45. fflush (stdout) ;
  46. start = millis () ;
  47. for (count = 0 ; count < FAST_COUNT ; ++count)
  48. digitalWrite (0, 1) ;
  49. end = millis () ;
  50. printf (" %8dmS\n", end - start) ;
  51. sum += (end - start) ;
  52. }
  53. digitalWrite (0, 0) ;
  54. printf (" Average: %8dmS", sum / 3) ;
  55. perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
  56. printf (": %6d/sec\n", perSec) ;
  57. printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ;
  58. if (wiringPiSetupGpio () == -1)
  59. exit (1) ;
  60. pinMode (17, OUTPUT) ;
  61. sum = 0 ;
  62. for (i = 0 ; i < 3 ; ++i)
  63. {
  64. printf (" Pass: %d: ", i) ;
  65. fflush (stdout) ;
  66. start = millis () ;
  67. for (count = 0 ; count < 10000000 ; ++count)
  68. digitalWrite (17, 1) ;
  69. end = millis () ;
  70. printf (" %8dmS\n", end - start) ;
  71. sum += (end - start) ;
  72. }
  73. digitalWrite (17, 0) ;
  74. printf (" Average: %8dmS", sum / 3) ;
  75. perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
  76. printf (": %6d/sec\n", perSec) ;
  77. // Switch to SYS mode:
  78. if (wiringPiSetupSys () == -1)
  79. exit (1) ;
  80. printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
  81. sum = 0 ;
  82. for (i = 0 ; i < 3 ; ++i)
  83. {
  84. printf (" Pass: %d: ", i) ;
  85. fflush (stdout) ;
  86. start = millis () ;
  87. for (count = 0 ; count < SLOW_COUNT ; ++count)
  88. digitalWrite (17, 1) ;
  89. end = millis () ;
  90. printf (" %8dmS\n", end - start) ;
  91. sum += (end - start) ;
  92. }
  93. digitalWrite (17, 0) ;
  94. printf (" Average: %8dmS", sum / 3) ;
  95. perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
  96. printf (": %6d/sec\n", perSec) ;
  97. return 0 ;
  98. }