chiark / gitweb /
Quite a few changes here.
[wiringPi.git] / examples / speed.c
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
26 #include <wiringPi.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31
32 #define FAST_COUNT      10000000
33 #define SLOW_COUNT       1000000
34
35
36 int main (void)
37 {
38   int i ;
39   uint32_t start, end, count, sum, perSec ;
40
41   printf ("Raspberry Pi wiringPi speed test program\n") ;
42
43 // Start the standard way
44
45   if (wiringPiSetup () == -1)
46     exit (1) ;
47
48   printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
49
50   pinMode (0, OUTPUT) ;
51
52   sum = 0 ;
53   for (i = 0 ; i < 3 ; ++i)
54   {
55     printf ("  Pass: %d: ", i) ;
56     fflush (stdout) ;
57
58     start = millis () ;
59     for (count = 0 ; count < FAST_COUNT ; ++count)
60       digitalWrite (0, 1) ;
61     end = millis () ;
62     printf (" %8dmS\n", end - start) ;
63     sum += (end - start) ;
64   }
65   digitalWrite (0, 0) ;
66   printf ("   Average: %8dmS", sum / 3) ;
67   perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
68   printf (": %6d/sec\n", perSec) ;
69
70
71   printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ;
72
73   if (wiringPiSetupGpio () == -1)
74     exit (1) ;
75
76   pinMode (17, OUTPUT) ;
77
78   sum = 0 ;
79   for (i = 0 ; i < 3 ; ++i)
80   {
81     printf ("  Pass: %d: ", i) ;
82     fflush (stdout) ;
83
84     start = millis () ;
85     for (count = 0 ; count < 10000000 ; ++count)
86       digitalWrite (17, 1) ;
87     end = millis () ;
88     printf (" %8dmS\n", end - start) ;
89     sum += (end - start) ;
90   }
91   digitalWrite (17, 0) ;
92   printf ("   Average: %8dmS", sum / 3) ;
93   perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
94   printf (": %6d/sec\n", perSec) ;
95
96
97 // Switch to SYS mode:
98
99   if (wiringPiSetupSys () == -1)
100     exit (1) ;
101
102   printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
103
104   sum = 0 ;
105   for (i = 0 ; i < 3 ; ++i)
106   {
107     printf ("  Pass: %d: ", i) ;
108     fflush (stdout) ;
109
110     start = millis () ;
111     for (count = 0 ; count < SLOW_COUNT ; ++count)
112       digitalWrite (17, 1) ;
113     end = millis () ;
114     printf (" %8dmS\n", end - start) ;
115     sum += (end - start) ;
116   }
117   digitalWrite (17, 0) ;
118   printf ("   Average: %8dmS", sum / 3) ;
119   perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
120   printf (": %6d/sec\n", perSec) ;
121
122   return 0 ;
123 }