bench.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. GCC
  2. Back to the Win32 Shootout
  3. Back to dada's perl lab
  4. [The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform
  5. Details]   [Acknowledgements]   [Scorecard]  
  6. All Source For gcc
  7. Ackermann's Function
  8. /* -*- mode: c -*-
  9. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  10. * http://www.bagley.org/~doug/shootout/
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. int
  15. Ack(int M, int N) {
  16. if (M == 0) return( N + 1 );
  17. if (N == 0) return( Ack(M - 1, 1) );
  18. return( Ack(M - 1, Ack(M, (N - 1))) );
  19. }
  20. int
  21. main(int argc, char *argv[]) {
  22. int n = ((argc == 2) ? atoi(argv[1]) : 1);
  23. printf("Ack(3,%d): %d\n", n, Ack(3, n));
  24. return(0);
  25. }
  26. Array Access
  27. /* -*- mode: c -*-
  28. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  29. * http://www.bagley.org/~doug/shootout/
  30. *
  31. * this program is modified from:
  32. * http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
  33. * Timing Trials, or, the Trials of Timing: Experiments with Scripting
  34. * and User-Interface Languages</a> by Brian W. Kernighan and
  35. * Christopher J. Van Wyk.
  36. *
  37. * I added free() to deallocate memory.
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. int
  42. main(int argc, char *argv[]) {
  43. int n = ((argc == 2) ? atoi(argv[1]) : 1);
  44. int i, k, *x, *y;
  45. x = (int *) calloc(n, sizeof(int));
  46. y = (int *) calloc(n, sizeof(int));
  47. for (i = 0; i < n; i++) {
  48. x[i] = i + 1;
  49. }
  50. for (k=0; k<1000; k++) {
  51. for (i = n-1; i >= 0; i--) {
  52. y[i] += x[i];
  53. }
  54. }
  55. fprintf(stdout, "%d %d\n", y[0], y[n-1]);
  56. free(x);
  57. free(y);
  58. return(0);
  59. }
  60. Count Lines/Words/Chars
  61. /* -*- mode: c -*-
  62. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  63. * http://www.bagley.org/~doug/shootout/
  64. *
  65. * this program is modified from:
  66. * http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
  67. * Timing Trials, or, the Trials of Timing: Experiments with Scripting
  68. * and User-Interface Languages</a> by Brian W. Kernighan and
  69. * Christopher J. Van Wyk.
  70. *
  71. */
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include <unistd.h>
  75. #define IN 1
  76. #define OUT 0
  77. int
  78. main() {
  79. int i, c, nl, nw, nc, state, nread;
  80. char buf[4096];
  81. state = OUT;
  82. nl = nw = nc = 0;
  83. while ((nread = read(0, buf, sizeof(buf))) > 0) {
  84. nc += nread;
  85. for (i=0; i<nread; i++) {
  86. c = buf[i];
  87. if (c == '\n')
  88. ++nl;
  89. if (c == ' ' || c == '\n' || c == '\t')
  90. state = OUT;
  91. else if (state == OUT) {
  92. state = IN;
  93. ++nw;
  94. }
  95. }
  96. }
  97. printf("%d %d %d\n", nl, nw, nc);
  98. return(0);
  99. }
  100. Echo Client/Server
  101. /* -*- mode: c -*-
  102. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  103. * http://www.bagley.org/~doug/shootout/
  104. */
  105. #include <stdio.h>
  106. #include <stdlib.h>
  107. #include <string.h>
  108. #include <unistd.h>
  109. #include <signal.h>
  110. #include <errno.h>
  111. #include <sys/types.h>
  112. #include <sys/socket.h>
  113. #include <sys/wait.h>
  114. #include <netinet/in.h>
  115. typedef int (*SOCKACTION_P)(int,struct sockaddr *,socklen_t);
  116. #define DATA "Hello there sailor\n"
  117. void myabort (char *m) { fprintf(stderr, "%s\n", m); exit(1); }
  118. void sysabort (char *m) { perror(m); exit(1); }
  119. int sigchld = 0;
  120. void reaper (int sig) { sigchld = 1; }
  121. int
  122. genericSock(int port,SOCKACTION_P action,char *actionExceptionText) {
  123. int ss, optval = 1;
  124. struct sockaddr_in sin;
  125. if ((ss = socket(PF_INET, SOCK_STREAM, 0)) == -1)
  126. sysabort("socket");
  127. if (setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) ==
  128. -1)
  129. sysabort("setsockopt");
  130. memset(&sin,0,sizeof(sin));
  131. sin.sin_family = AF_INET;
  132. sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  133. sin.sin_port = port;
  134. if (action(ss, (struct sockaddr *)&sin,(socklen_t)sizeof(sin)) == -1)
  135. sysabort(actionExceptionText);
  136. return(ss);
  137. }
  138. int
  139. server_sock () {
  140. int ss = genericSock(0,(SOCKACTION_P)bind,"server/bind");
  141. return(listen(ss,2),ss);
  142. }
  143. int
  144. client_sock (int port) {
  145. return(genericSock(port,(SOCKACTION_P)connect,"client/connect"));
  146. }
  147. int
  148. get_port (int sock) {
  149. struct sockaddr_in sin;
  150. socklen_t slen = sizeof(sin);
  151. if (getsockname(sock, (struct sockaddr *)&sin, &slen) == -1)
  152. sysabort("server/getsockname");
  153. return(sin.sin_port);
  154. }
  155. void
  156. echo_client (int n, int port) {
  157. int i, sock, olen, len, nwritten, nread;
  158. char *offset, obuf[64], ibuf[64];
  159. char *end = ibuf + sizeof(ibuf);
  160. sock = client_sock(port);
  161. strcpy(obuf, DATA);
  162. olen = strlen(obuf);
  163. for (i=0; i<n; i++) {
  164. len = olen;
  165. offset = obuf;
  166. while (len > 0) {
  167. if ((nwritten = write(sock, offset, len)) == -1)
  168. sysabort("client/write");
  169. offset += nwritten;
  170. len -= nwritten;
  171. }
  172. offset = ibuf;
  173. while ((nread = read(sock, offset, (end - offset))) > 0) {
  174. offset += nread;
  175. if (*(offset-1) == '\n') break;
  176. }
  177. if (nread == -1)
  178. sysabort("client/read");
  179. *offset = 0;
  180. if ((strcmp(obuf, ibuf)) != 0) {
  181. char mbuf[128];
  182. sprintf(mbuf, "client: \"%s\" ne \"%s\"", obuf, ibuf);
  183. myabort(mbuf);
  184. }
  185. }
  186. close(sock);
  187. }
  188. void
  189. echo_server (int n) {
  190. int ssock, csock, len, nwritten, total_bytes;
  191. pid_t pid;
  192. char buf[64], *offset;
  193. struct sockaddr_in sin;
  194. socklen_t slen = sizeof(sin);
  195. int status;
  196. ssock = server_sock();
  197. signal(SIGCHLD, reaper);
  198. if ((pid = fork()) == -1)
  199. sysabort("server/fork");
  200. if (pid) {
  201. if ((csock = accept(ssock, (struct sockaddr *)&sin, &slen)) == -1)
  202. sysabort("server/accept");
  203. total_bytes = 0;
  204. while ((len = read(csock, buf, sizeof(buf))) > 0) {
  205. if (sigchld) myabort("server/sigchld");
  206. offset = buf;
  207. total_bytes += len;
  208. while (len > 0) {
  209. if ((nwritten = write(csock, offset, len)) == -1)
  210. sysabort("server/write");
  211. offset += nwritten;
  212. len -= nwritten;
  213. }
  214. }
  215. if (len == -1)
  216. sysabort("server/read");
  217. close(csock);
  218. fprintf(stdout, "server processed %d bytes\n", total_bytes);
  219. } else {
  220. echo_client(n, get_port(ssock));
  221. }
  222. wait(&status);
  223. }
  224. int
  225. main(int argc, char *argv[]) {
  226. echo_server((argc == 2) ? atoi(argv[1]) : 1);
  227. return(0);
  228. }
  229. Exception Mechanisms
  230. /* -*- mode: c -*-
  231. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  232. * http://www.bagley.org/~doug/shootout/
  233. */
  234. #include <stdio.h>
  235. #include <stdlib.h>
  236. #include <setjmp.h>
  237. int HI = 0, LO = 0;
  238. static jmp_buf Hi_exception;
  239. static jmp_buf Lo_exception;
  240. void blowup (int n) {
  241. if (n & 1) {
  242. longjmp(Lo_exception, 1);
  243. } else {
  244. longjmp(Hi_exception, 1);
  245. }
  246. }
  247. void lo_function (volatile int n) {
  248. if (setjmp(Lo_exception) != 0) {
  249. LO++;
  250. } else {
  251. blowup(n);
  252. }
  253. }
  254. void hi_function (volatile int n) {
  255. if (setjmp(Hi_exception) != 0) {
  256. HI++;
  257. } else {
  258. lo_function(n);
  259. }
  260. }
  261. void some_function (int n) {
  262. hi_function(n);
  263. }
  264. int
  265. main(int argc, char *argv[]) {
  266. int volatile N = ((argc == 2) ? atoi(argv[1]) : 1);
  267. while (N) {
  268. some_function(N--);
  269. }
  270. printf("Exceptions: HI=%d / LO=%d\n", HI, LO);
  271. return(0);
  272. }
  273. Fibonacci Numbers
  274. /* -*- mode: c -*-
  275. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  276. * http://www.bagley.org/~doug/shootout/
  277. */
  278. #include <stdio.h>
  279. #include <stdlib.h>
  280. unsigned long
  281. fib(unsigned long n) {
  282. if (n < 2)
  283. return(1);
  284. else
  285. return(fib(n-2) + fib(n-1));
  286. }
  287. int
  288. main(int argc, char *argv[]) {
  289. int N = ((argc == 2) ? atoi(argv[1]) : 1);
  290. printf("%ld\n", fib(N));
  291. return(0);
  292. }
  293. Hash (Associative Array) Access
  294. /* -*- mode: c -*-
  295. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  296. * http://www.bagley.org/~doug/shootout/
  297. */
  298. #include <stdio.h>
  299. #include <stdlib.h>
  300. #include <unistd.h>
  301. #include "simple_hash.h"
  302. int main(int argc, char *argv[]) {
  303. int i, c=0, n = ((argc == 2) ? atoi(argv[1]) : 1);
  304. char buf[32];
  305. struct ht_ht *ht = ht_create(n);
  306. for (i=1; i<=n; i++) {
  307. sprintf(buf, "%x", i);
  308. (ht_find_new(ht, buf))->val = i;
  309. }
  310. for (i=n; i>0; i--) {
  311. sprintf(buf, "%d", i);
  312. if (ht_find(ht, buf)) c++;
  313. }
  314. ht_destroy(ht);
  315. printf("%d\n", c);
  316. return(0);
  317. }
  318. Hashes, Part II
  319. /* -*- mode: c -*-
  320. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  321. * http://www.bagley.org/~doug/shootout/
  322. */
  323. #include <stdio.h>
  324. #include <stdlib.h>
  325. #include <unistd.h>
  326. #include "simple_hash.h"
  327. int main(int argc, char *argv[]) {
  328. int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
  329. char buf[32];
  330. struct ht_ht *ht1 = ht_create(10000);
  331. struct ht_ht *ht2 = ht_create(10000);
  332. struct ht_node *node;
  333. for (i=0; i<=9999; ++i) {
  334. sprintf(buf, "foo_%d", i);
  335. ht_find_new(ht1, buf)->val = i;
  336. }
  337. for (i=0; i<n; ++i) {
  338. for (node=ht_first(ht1); node; node=ht_next(ht1)) {
  339. ht_find_new(ht2, node->key)->val += node->val;
  340. }
  341. }
  342. printf("%d %d %d %d\n",
  343. (ht_find(ht1, "foo_1"))->val,
  344. (ht_find(ht1, "foo_9999"))->val,
  345. (ht_find(ht2, "foo_1"))->val,
  346. (ht_find(ht2, "foo_9999"))->val);
  347. ht_destroy(ht1);
  348. ht_destroy(ht2);
  349. return(0);
  350. }
  351. Heapsort
  352. /* -*- mode: c -*-
  353. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  354. * http://www.bagley.org/~doug/shootout/
  355. */
  356. #include <stdlib.h>
  357. #include <math.h>
  358. #include <stdio.h>
  359. #define IM 139968
  360. #define IA 3877
  361. #define IC 29573
  362. double
  363. gen_random(double max) {
  364. static long last = 42;
  365. return( max * (last = (last * IA + IC) % IM) / IM );
  366. }
  367. void
  368. heapsort(int n, double *ra) {
  369. int i, j;
  370. int ir = n;
  371. int l = (n >> 1) + 1;
  372. double rra;
  373. for (;;) {
  374. if (l > 1) {
  375. rra = ra[--l];
  376. } else {
  377. rra = ra[ir];
  378. ra[ir] = ra[1];
  379. if (--ir == 1) {
  380. ra[1] = rra;
  381. return;
  382. }
  383. }
  384. i = l;
  385. j = l << 1;
  386. while (j <= ir) {
  387. if (j < ir && ra[j] < ra[j+1]) { ++j; }
  388. if (rra < ra[j]) {
  389. ra[i] = ra[j];
  390. j += (i = j);
  391. } else {
  392. j = ir + 1;
  393. }
  394. }
  395. ra[i] = rra;
  396. }
  397. }
  398. int
  399. main(int argc, char *argv[]) {
  400. int N = ((argc == 2) ? atoi(argv[1]) : 1);
  401. double *ary;
  402. int i;
  403. ary = (double *)malloc((N+1) * sizeof(double));
  404. for (i=1; i<=N; i++) {
  405. ary[i] = gen_random(1);
  406. }
  407. heapsort(N, ary);
  408. printf("%.10g\n", ary[N]);
  409. free(ary);
  410. return(0);
  411. }
  412. Hello World
  413. /* -*- mode: c -*-
  414. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  415. * http://www.bagley.org/~doug/shootout/
  416. */
  417. #include <stdio.h>
  418. int main() {
  419. fputs("hello world\n", stdout);
  420. return(0);
  421. }
  422. List Operations
  423. /* -*- mode: c -*-
  424. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  425. * http://www.bagley.org/~doug/shootout/
  426. */
  427. #include <stdio.h>
  428. #include <stdlib.h>
  429. #include <string.h>
  430. #include <unistd.h>
  431. #define SIZE 10000
  432. // a simple Double Linked List
  433. // the head node is special, it's val is length of list
  434. typedef struct DLL {
  435. int val;
  436. struct DLL *next;
  437. struct DLL *prev;
  438. } DLL;
  439. inline int list_length(DLL *head) { return(head->val); }
  440. inline int list_empty(DLL *head) { return(list_length(head) == 0); }
  441. inline DLL *list_first(DLL *head) { return(head->next); }
  442. inline DLL *list_last(DLL *head) { return(head->prev); }
  443. void list_push_tail(DLL *head, DLL *item) {
  444. DLL *tail = head->prev;
  445. tail->next = item;
  446. item->next = head;
  447. head->prev = item;
  448. item->prev = tail;
  449. head->val++;
  450. }
  451. DLL *list_pop_tail(DLL *head) {
  452. DLL *prev, *tail;
  453. if (list_empty(head)) return(NULL);
  454. tail = head->prev;
  455. prev = tail->prev;
  456. prev->next = head;
  457. head->prev = prev;
  458. head->val--;
  459. return(tail);
  460. }
  461. void list_push_head(DLL *head, DLL *item) {
  462. DLL *next = head->next;
  463. head->next = item;
  464. next->prev = item;
  465. item->next = next;
  466. item->prev = head;
  467. head->val++;
  468. }
  469. DLL *list_pop_head(DLL *head) {
  470. DLL *next;
  471. if (list_empty(head)) return(NULL);
  472. next = head->next;
  473. head->next = next->next;
  474. next->next->prev = head;
  475. head->val--;
  476. return(next);
  477. }
  478. int list_equal(DLL *x, DLL *y) {
  479. DLL *xp, *yp;
  480. // first val's checked will be list lengths
  481. for (xp=x, yp=y; xp->next != x; xp=xp->next, yp=yp->next) {
  482. if (xp->val != yp->val) return(0);
  483. }
  484. if (xp->val != yp->val) return(0);
  485. return(yp->next == y);
  486. }
  487. void list_print(char *msg, DLL *x) {
  488. DLL *xp, *first = x->next;
  489. int i = 0;
  490. printf(msg);
  491. printf("length: %d\n", list_length(x));
  492. for (xp=x->next; xp->next != first; xp=xp->next) {
  493. printf("i:%3d v:%3d n:%3d p:%3d\n", ++i,
  494. xp->val, xp->next->val, xp->prev->val);
  495. }
  496. printf("[last entry points to list head]\n");
  497. printf("[val of next of tail is: %d]\n", xp->next->val);
  498. }
  499. DLL *list_new() {
  500. DLL *l = (DLL *)malloc(sizeof(DLL));
  501. l->next = l;
  502. l->prev = l;
  503. l->val = 0;
  504. return(l);
  505. }
  506. DLL *list_sequence(int from, int to) {
  507. int size, tmp, i, j;
  508. DLL *l;
  509. if (from > to) {
  510. tmp = from; from = to; to = tmp;
  511. }
  512. size = to - from + 1;
  513. l = (DLL *)malloc((size+1) * sizeof(DLL));
  514. from--;
  515. for (i=0, j=1; i<size; ++i, ++j) {
  516. l[i].next = &l[i+1];
  517. l[j].prev = &l[j-1];
  518. l[i].val = from++;
  519. }
  520. l[0].prev = &l[size];
  521. l[size].next = &l[0];
  522. l[size].prev = &l[size-1];
  523. l[size].val = from;
  524. l[0].val = size;
  525. return(l);
  526. }
  527. DLL *list_copy(DLL *x) {
  528. int i, j, size = list_length(x);
  529. DLL *xp, *l = (DLL *)malloc((size+1) * sizeof(DLL));
  530. for (i=0, j=1, xp=x; i<size; i++, j++, xp=xp->next) {
  531. l[i].next = &l[j];
  532. l[j].prev = &l[i];
  533. l[i].val = xp->val;
  534. }
  535. l[0].prev = &l[size];
  536. l[size].next = &l[0];
  537. l[size].val = list_last(x)->val;
  538. return(l);
  539. }
  540. void list_reverse (DLL *head) {
  541. DLL *tmp, *p = head;
  542. do {
  543. tmp = p->next;
  544. p->next = p->prev;
  545. p->prev = tmp;
  546. p = tmp;
  547. } while (p != head);
  548. }
  549. int test_lists() {
  550. int len = 0;
  551. // create a list of integers (li1) from 1 to SIZE
  552. DLL *li1 = list_sequence(1, SIZE);
  553. // copy the list to li2
  554. DLL *li2 = list_copy(li1);
  555. // remove each individual item from left side of li2 and
  556. // append to right side of li3 (preserving order)
  557. DLL *li3 = list_new();
  558. // compare li2 and li1 for equality
  559. if (!list_equal(li2, li1)) {
  560. fprintf(stderr, "li2 and li1 are not equal\n");
  561. exit(1);
  562. }
  563. while (!list_empty(li2)) {
  564. list_push_tail(li3, list_pop_head(li2));
  565. }
  566. // li2 must now be empty
  567. if (!list_empty(li2)) {
  568. fprintf(stderr, "li2 should be empty now\n");
  569. exit(1);
  570. }
  571. // remove each individual item from right side of li3 and
  572. // append to right side of li2 (reversing list)
  573. while (!list_empty(li3)) {
  574. list_push_tail(li2, list_pop_tail(li3));
  575. }
  576. // li3 must now be empty
  577. if (!list_empty(li3)) {
  578. fprintf(stderr, "li3 should be empty now\n");
  579. exit(1);
  580. }
  581. // reverse li1 in place
  582. list_reverse(li1);
  583. // check that li1's first item is now SIZE
  584. if (list_first(li1)->val != SIZE) {
  585. fprintf(stderr, "li1 first value wrong, wanted %d, got %d\n",
  586. SIZE, list_first(li1)->val);
  587. exit(1);
  588. }
  589. // check that li1's last item is now 1
  590. if (list_last(li1)->val != 1) {
  591. fprintf(stderr, "last value wrong, wanted %d, got %d\n",
  592. SIZE, list_last(li1)->val);
  593. exit(1);
  594. }
  595. // check that li2's first item is now SIZE
  596. if (list_first(li2)->val != SIZE) {
  597. fprintf(stderr, "li2 first value wrong, wanted %d, got %d\n",
  598. SIZE, list_first(li2)->val);
  599. exit(1);
  600. }
  601. // check that li2's last item is now 1
  602. if (list_last(li2)->val != 1) {
  603. fprintf(stderr, "last value wrong, wanted %d, got %d\n",
  604. SIZE, list_last(li2)->val);
  605. exit(1);
  606. }
  607. // check that li1's length is still SIZE
  608. if (list_length(li1) != SIZE) {
  609. fprintf(stderr, "li1 size wrong, wanted %d, got %d\n",
  610. SIZE, list_length(li1));
  611. exit(1);
  612. }
  613. // compare li1 and li2 for equality
  614. if (!list_equal(li1, li2)) {
  615. fprintf(stderr, "li1 and li2 are not equal\n");
  616. exit(1);
  617. }
  618. len = list_length(li1);
  619. free(li1);
  620. free(li2);
  621. free(li3);
  622. // return the length of the list
  623. return(len);
  624. }
  625. int main(int argc, char *argv[]) {
  626. int n = ((argc == 2) ? atoi(argv[1]) : 1);
  627. int result = 0;
  628. while(n--) result = test_lists();
  629. printf("%d\n", result);
  630. return 0;
  631. }
  632. Matrix Multiplication
  633. /* -*- mode: c -*-
  634. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  635. * http://www.bagley.org/~doug/shootout/
  636. */
  637. #include <stdio.h>
  638. #include <stdlib.h>
  639. #include <unistd.h>
  640. #define SIZE 30
  641. int **mkmatrix(int rows, int cols) {
  642. int i, j, count = 1;
  643. int **m = (int **) malloc(rows * sizeof(int *));
  644. for (i=0; i<rows; i++) {
  645. m[i] = (int *) malloc(cols * sizeof(int));
  646. for (j=0; j<cols; j++) {
  647. m[i][j] = count++;
  648. }
  649. }
  650. return(m);
  651. }
  652. void zeromatrix(int rows, int cols, int **m) {
  653. int i, j;
  654. for (i=0; i<rows; i++)
  655. for (j=0; j<cols; j++)
  656. m[i][j] = 0;
  657. }
  658. void freematrix(int rows, int **m) {
  659. while (--rows > -1) { free(m[rows]); }
  660. free(m);
  661. }
  662. int **mmult(int rows, int cols, int **m1, int **m2, int **m3) {
  663. int i, j, k, val;
  664. for (i=0; i<rows; i++) {
  665. for (j=0; j<cols; j++) {
  666. val = 0;
  667. for (k=0; k<cols; k++) {
  668. val += m1[i][k] * m2[k][j];
  669. }
  670. m3[i][j] = val;
  671. }
  672. }
  673. return(m3);
  674. }
  675. int main(int argc, char *argv[]) {
  676. int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
  677. int **m1 = mkmatrix(SIZE, SIZE);
  678. int **m2 = mkmatrix(SIZE, SIZE);
  679. int **mm = mkmatrix(SIZE, SIZE);
  680. for (i=0; i<n; i++) {
  681. mm = mmult(SIZE, SIZE, m1, m2, mm);
  682. }
  683. printf("%d %d %d %d\n", mm[0][0], mm[2][3], mm[3][2], mm[4][4]);
  684. freematrix(SIZE, m1);
  685. freematrix(SIZE, m2);
  686. freematrix(SIZE, mm);
  687. return(0);
  688. }
  689. Method Calls
  690. /* -*- mode: c -*-
  691. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  692. * http://www.bagley.org/~doug/shootout/
  693. */
  694. #include <stdio.h>
  695. #include <stdlib.h>
  696. #define true 1
  697. #define false 0
  698. #define TOGGLE \
  699. char state; \
  700. char (*value)(struct Toggle *); \
  701. struct Toggle *(*activate)(struct Toggle *)
  702. #define DESTROY free
  703. typedef struct Toggle {
  704. TOGGLE;
  705. } Toggle;
  706. char toggle_value(Toggle *this) {
  707. return(this->state);
  708. }
  709. Toggle *toggle_activate(Toggle *this) {
  710. this->state = !this->state;
  711. return(this);
  712. }
  713. Toggle *init_Toggle(Toggle *this, char start_state) {
  714. this->state = start_state;
  715. this->value = toggle_value;
  716. this->activate = toggle_activate;
  717. return(this);
  718. }
  719. Toggle *new_Toggle(char start_state) {
  720. Toggle *this = (Toggle *)malloc(sizeof(Toggle));
  721. return(init_Toggle(this, start_state));
  722. }
  723. typedef struct NthToggle {
  724. TOGGLE;
  725. int count_max;
  726. int counter;
  727. } NthToggle;
  728. NthToggle *nth_toggle_activate(NthToggle *this) {
  729. if (++this->counter >= this->count_max) {
  730. this->state = !this->state;
  731. this->counter = 0;
  732. }
  733. return(this);
  734. }
  735. NthToggle *init_NthToggle(NthToggle *this, int max_count) {
  736. this->count_max = max_count;
  737. this->counter = 0;
  738. this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
  739. return(this);
  740. }
  741. NthToggle *new_NthToggle(char start_state, int max_count) {
  742. NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
  743. this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
  744. return(init_NthToggle(this, max_count));
  745. }
  746. int main(int argc, char *argv[]) {
  747. int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
  748. Toggle *tog;
  749. NthToggle *ntog;
  750. char val = true;
  751. tog = new_Toggle(true);
  752. for (i=0; i<n; i++) {
  753. val = tog->activate(tog)->value(tog);
  754. }
  755. fputs(val ? "true\n" : "false\n", stdout);
  756. DESTROY(tog);
  757. val = true;
  758. ntog = new_NthToggle(val, 3);
  759. for (i=0; i<n; i++) {
  760. val = ntog->activate(ntog)->value(ntog);
  761. }
  762. fputs(val ? "true\n" : "false\n", stdout);
  763. DESTROY(ntog);
  764. return 0;
  765. }
  766. Nested Loops
  767. /* -*- mode: c -*-
  768. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  769. * http://www.bagley.org/~doug/shootout/
  770. */
  771. #include <stdio.h>
  772. #include <stdlib.h>
  773. #include <unistd.h>
  774. int
  775. main(int argc, char *argv[]) {
  776. int n = ((argc == 2) ? atoi(argv[1]) : 1);
  777. int a, b, c, d, e, f, x=0;
  778. for (a=0; a<n; a++)
  779. for (b=0; b<n; b++)
  780. for (c=0; c<n; c++)
  781. for (d=0; d<n; d++)
  782. for (e=0; e<n; e++)
  783. for (f=0; f<n; f++)
  784. x++;
  785. printf("%d\n", x);
  786. return(0);
  787. }
  788. Object Instantiation
  789. /* -*- mode: c -*-
  790. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  791. * http://www.bagley.org/~doug/shootout/
  792. */
  793. #include <stdio.h>
  794. #include <stdlib.h>
  795. enum {false, true};
  796. #define TOGGLE \
  797. char state; \
  798. char (*value)(struct Toggle *); \
  799. struct Toggle *(*activate)(struct Toggle *)
  800. #define DESTROY free
  801. typedef struct Toggle {
  802. TOGGLE;
  803. } Toggle;
  804. char toggle_value(Toggle *this) {
  805. return(this->state);
  806. }
  807. Toggle *toggle_activate(Toggle *this) {
  808. this->state = !this->state;
  809. return(this);
  810. }
  811. Toggle *init_Toggle(Toggle *this, char start_state) {
  812. this->state = start_state;
  813. this->value = toggle_value;
  814. this->activate = toggle_activate;
  815. return(this);
  816. }
  817. Toggle *new_Toggle(char start_state) {
  818. Toggle *this = (Toggle *)malloc(sizeof(Toggle));
  819. return(init_Toggle(this, start_state));
  820. }
  821. typedef struct NthToggle {
  822. TOGGLE;
  823. int count_max;
  824. int counter;
  825. } NthToggle;
  826. NthToggle *nth_toggle_activate(NthToggle *this) {
  827. if (++this->counter >= this->count_max) {
  828. this->state = !this->state;
  829. this->counter = 0;
  830. }
  831. return(this);
  832. }
  833. NthToggle *init_NthToggle(NthToggle *this, int max_count) {
  834. this->count_max = max_count;
  835. this->counter = 0;
  836. this->activate = (Toggle *(*)(Toggle *))nth_toggle_activate;
  837. return(this);
  838. }
  839. NthToggle *new_NthToggle(char start_state, int max_count) {
  840. NthToggle *this = (NthToggle *)malloc(sizeof(NthToggle));
  841. this = (NthToggle *)init_Toggle((Toggle *)this, start_state);
  842. return(init_NthToggle(this, max_count));
  843. }
  844. int main(int argc, char *argv[]) {
  845. int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
  846. Toggle *tog;
  847. NthToggle *ntog;
  848. tog = new_Toggle(true);
  849. for (i=0; i<5; i++) {
  850. fputs((tog->activate(tog)->value(tog)) ? "true\n" : "false\n", stdout);
  851. }
  852. DESTROY(tog);
  853. for (i=0; i<n; i++) {
  854. tog = new_Toggle(true);
  855. DESTROY(tog);
  856. }
  857. fputs("\n", stdout);
  858. ntog = new_NthToggle(true, 3);
  859. for (i=0; i<8; i++) {
  860. fputs((ntog->activate(ntog)->value(ntog)) ? "true\n" : "false\n",
  861. stdout);
  862. }
  863. DESTROY(ntog);
  864. for (i=0; i<n; i++) {
  865. ntog = new_NthToggle(true, 3);
  866. DESTROY(ntog);
  867. }
  868. return 0;
  869. }
  870. Producer/Consumer Threads
  871. /* -*- mode: c -*-
  872. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  873. * http://www.bagley.org/~doug/shootout/
  874. */
  875. #include <stdio.h>
  876. #include <stdlib.h>
  877. #include <string.h>
  878. #include <unistd.h>
  879. #include <signal.h>
  880. #include <errno.h>
  881. #include <sys/types.h>
  882. #include <pthread.h>
  883. pthread_mutex_t mutex;
  884. pthread_cond_t control;
  885. void producer(unsigned int *arg);
  886. void consumer(unsigned int *arg);
  887. unsigned int count, data, consumed, produced;
  888. int
  889. main(int argc, char *argv[]) {
  890. unsigned int n = ((argc == 2) ? atoi(argv[1]) : 1);
  891. pthread_t t1, t2;
  892. count = data = consumed = produced = 0;
  893. if (pthread_mutex_init(&mutex, NULL)) {
  894. perror("pthread_mutex_init");
  895. exit(1);
  896. }
  897. if (pthread_cond_init(&control, NULL)) {
  898. perror("pthread_cond_init");
  899. exit(1);
  900. }
  901. if (pthread_create(&t1, (pthread_attr_t *)NULL,
  902. (void * (*)(void *))producer, (void *)&n)) {
  903. perror("pthread_create");
  904. exit(1);
  905. }
  906. if (pthread_create(&t2, (pthread_attr_t *)NULL,
  907. (void * (*)(void *))consumer, (void *)&n)) {
  908. perror("pthread_create");
  909. exit(1);
  910. }
  911. pthread_join(t1, NULL);
  912. pthread_join(t2, NULL);
  913. fprintf(stdout, "%d %d\n", produced, consumed);
  914. return(0);
  915. }
  916. void producer(unsigned int *arg) {
  917. unsigned int i, n = *arg;
  918. for (i=1; i<=n; i++) {
  919. pthread_mutex_lock(&mutex);
  920. while (count == 1) {
  921. pthread_cond_wait(&control, &mutex);
  922. }
  923. data = i;
  924. count = 1;
  925. pthread_cond_signal(&control);
  926. pthread_mutex_unlock(&mutex);
  927. produced++;
  928. }
  929. }
  930. void consumer(unsigned int *arg) {
  931. unsigned int i = 0, n = *arg;
  932. while (1) {
  933. pthread_mutex_lock(&mutex);
  934. while (count == 0) {
  935. pthread_cond_wait(&control, &mutex);
  936. }
  937. i = data;
  938. count = 0;
  939. pthread_cond_signal(&control);
  940. pthread_mutex_unlock(&mutex);
  941. consumed++;
  942. if (i == n) return;
  943. }
  944. }
  945. Random Number Generator
  946. /* -*- mode: c -*-
  947. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  948. * http://www.bagley.org/~doug/shootout/
  949. */
  950. #include <math.h>
  951. #include <stdio.h>
  952. #include <stdlib.h>
  953. #define IM 139968
  954. #define IA 3877
  955. #define IC 29573
  956. double
  957. gen_random(double max) {
  958. static long last = 42;
  959. last = (last * IA + IC) % IM;
  960. return( max * last / IM );
  961. }
  962. int
  963. main(int argc, char *argv[]) {
  964. int N = ((argc == 2) ? atoi(argv[1]) : 1);
  965. double result = 0;
  966. while (N--) {
  967. result = gen_random(100.0);
  968. }
  969. printf("%.9f\n", result);
  970. return(0);
  971. }
  972. Regular Expression Matching
  973. /* -*- mode: c -*-
  974. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  975. * http://www.bagley.org/~doug/shootout/
  976. */
  977. #include <sys/types.h>
  978. #include <sys/stat.h>
  979. #include <fcntl.h>
  980. #include <stdio.h>
  981. #include <pcre.h>
  982. #include <string.h>
  983. #define MAXLINES 100
  984. #define MAXLINELEN 132
  985. char *pattern =
  986. "(?:^|[^\\d\\(])"
  987. "(\\()?"
  988. "(\\d\\d\\d)"
  989. "(?(1)\\))"
  990. "[ ]"
  991. "(\\d\\d\\d)"
  992. "[ -]"
  993. "(\\d\\d\\d\\d)"
  994. "\\D"
  995. ;
  996. int
  997. main(int argc, char *argv[]) {
  998. int NUM = ((argc == 2) ? atoi(argv[1]) : 1);
  999. int count;
  1000. char *cptr = "";
  1001. char **phones;
  1002. pcre *re;
  1003. int erroffset;
  1004. const char *errptr;
  1005. int n, lines = 0;
  1006. char num[256];
  1007. int i, j, k, matchlen;
  1008. char *matchoffset;
  1009. int nmatches;
  1010. int *ovec, ovecsize;
  1011. pcre_extra *study;
  1012. phones = (char **)malloc(MAXLINES * sizeof(char *));
  1013. if (!phones) {
  1014. fprintf(stderr, "malloc for phones array failed\n");
  1015. exit(1);
  1016. }
  1017. lines = 0;
  1018. while (cptr) {
  1019. phones[lines] = (char *)malloc(MAXLINELEN);
  1020. if (!phones[lines]) {
  1021. fprintf(stderr, "malloc to hold line #%d failed\n", lines);
  1022. exit(1);
  1023. }
  1024. cptr = fgets(phones[lines], MAXLINELEN, stdin);
  1025. lines++;
  1026. if (lines > MAXLINES) {
  1027. fprintf(stderr, "MAXLINES is too small\n");
  1028. exit(1);
  1029. }
  1030. }
  1031. re = pcre_compile(pattern, 0, &errptr, &erroffset, NULL);
  1032. if (!re) {
  1033. fprintf(stderr, "can't open compile regexp\n");
  1034. exit(1);
  1035. }
  1036. study = pcre_study(re, 0, &errptr);
  1037. if (pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &nmatches) != 0) {
  1038. fprintf(stderr, "pcre_fullinfo failed\n");
  1039. exit(1);
  1040. }
  1041. nmatches++;
  1042. ovecsize = sizeof(int) * nmatches * 3;
  1043. ovec = (int *)malloc(ovecsize);
  1044. if (!ovec) {
  1045. fprintf(stderr, "malloc for ovec array failed\n");
  1046. exit(1);
  1047. }
  1048. count = 0;
  1049. while (NUM--) {
  1050. for (i=0; i<lines; i++) {
  1051. n = pcre_exec(re, study,
  1052. phones[i], strlen(phones[i]), 0,
  1053. 0, ovec, ovecsize);
  1054. if (n == nmatches) {
  1055. k = 2*2;
  1056. j = 0;
  1057. num[j++] = '(';
  1058. matchoffset = phones[i] + ovec[k];
  1059. matchlen = ovec[k+1] - ovec[k];
  1060. strncpy(num+j, matchoffset, matchlen);
  1061. j += matchlen; k += 2;
  1062. num[j++] = ')';
  1063. num[j++] = ' ';
  1064. matchoffset = phones[i] + ovec[k];
  1065. matchlen = ovec[k+1] - ovec[k];
  1066. strncpy(num+j, matchoffset, matchlen);
  1067. j += matchlen; k += 2;
  1068. num[j++] = '-';
  1069. matchoffset = phones[i] + ovec[k];
  1070. matchlen = ovec[k+1] - ovec[k];
  1071. strncpy(num+j, matchoffset, matchlen);
  1072. j += matchlen; k += 2;
  1073. num[j] = 0;
  1074. if (0 == NUM) {
  1075. count++;
  1076. printf("%d: %s\n", count, num);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. for (i=0; i<MAXLINES; i++) {
  1082. free(phones[i]);
  1083. }
  1084. free(phones);
  1085. free(ovec);
  1086. return(0);
  1087. }
  1088. Reverse a File
  1089. /* -*- mode: c -*-
  1090. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1091. * http://www.bagley.org/~doug/shootout/
  1092. */
  1093. #include <stdio.h>
  1094. #include <stdlib.h>
  1095. #include <string.h>
  1096. #include <unistd.h>
  1097. #define MAXREAD 4096
  1098. int main(int argc, char *argv[]) {
  1099. int nread, len = 0, size = (4 * MAXREAD);
  1100. char *cp, *offset, *buf = malloc(size + 1);
  1101. while (1) {
  1102. if ((nread = read(0, (buf + len), MAXREAD)) > 0) {
  1103. len += nread;
  1104. if (MAXREAD > (size - len)) {
  1105. size <<= 1;
  1106. if (0 == (buf = realloc(buf, size + 1))) {
  1107. fprintf(stderr, "realloc failed\n");
  1108. exit(1);
  1109. }
  1110. }
  1111. } else {
  1112. if ( 0 == nread) break;
  1113. if (-1 == nread) {
  1114. perror("read");
  1115. exit(1);
  1116. }
  1117. }
  1118. }
  1119. offset = buf + len;
  1120. *offset = 0;
  1121. for (cp = offset; cp > buf; --cp) {
  1122. if ('\n' == *cp) {
  1123. *offset = 0;
  1124. if (cp < offset)
  1125. fputs(offset = cp+1, stdout);
  1126. }
  1127. }
  1128. if (cp < offset) {
  1129. *offset = 0;
  1130. fputs(cp, stdout);
  1131. }
  1132. free(buf);
  1133. return(0);
  1134. }
  1135. Sieve of Erathostenes
  1136. /* -*- mode: c -*-
  1137. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1138. * http://www.bagley.org/~doug/shootout/
  1139. */
  1140. #include <stdio.h>
  1141. #include <stdlib.h>
  1142. int
  1143. main(int argc, char *argv[]) {
  1144. int NUM = ((argc == 2) ? atoi(argv[1]) : 1);
  1145. static char flags[8192 + 1];
  1146. long i, k;
  1147. int count = 0;
  1148. while (NUM--) {
  1149. count = 0;
  1150. for (i=2; i <= 8192; i++) {
  1151. flags[i] = 1;
  1152. }
  1153. for (i=2; i <= 8192; i++) {
  1154. if (flags[i]) {
  1155. // remove all multiples of prime: i
  1156. for (k=i+i; k <= 8192; k+=i) {
  1157. flags[k] = 0;
  1158. }
  1159. count++;
  1160. }
  1161. }
  1162. }
  1163. printf("Count: %d\n", count);
  1164. return(0);
  1165. }
  1166. Spell Checker
  1167. /* -*- mode: c -*-
  1168. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1169. * http://www.bagley.org/~doug/shootout/
  1170. * with help from Brad Knotwell
  1171. */
  1172. #include <ctype.h>
  1173. #include <stdio.h>
  1174. #include <stdlib.h>
  1175. #include <unistd.h>
  1176. #include "simple_hash.h"
  1177. #define MAXLINELEN 128
  1178. struct ht_ht *dict = NULL;
  1179. int handleInput(FILE *input,void (*hashManipFn)(char *))
  1180. {
  1181. int wordbufsize = 80,i = 0;
  1182. char *cp, *wordbuf = (char *)malloc(wordbufsize + 1);
  1183. char line[MAXLINELEN];
  1184. if((wordbuf = malloc(wordbufsize+1)) == NULL)
  1185. return(fprintf(stderr,"malloc\n"),0);
  1186. while (fgets(line, MAXLINELEN, input))
  1187. for (cp=line; *cp > 0; cp++) {
  1188. if (isspace(*cp)) {
  1189. if (i) {
  1190. wordbuf[i] = '\0';
  1191. hashManipFn(wordbuf);
  1192. i = 0;
  1193. }
  1194. } else {
  1195. wordbuf[i++] = *cp;
  1196. if (i == wordbufsize) {
  1197. wordbufsize *= 2;
  1198. if((wordbuf = realloc(wordbuf, wordbufsize + 1)) == NULL)
  1199. return(fprintf(stderr, "realloc\n"), 0);
  1200. }
  1201. }
  1202. }
  1203. free(wordbuf);
  1204. return(1);
  1205. }
  1206. void spellCheck(char *key) {
  1207. if (ht_find_new(dict,key)->val != 1) printf("%s\n",key);
  1208. }
  1209. void hashLoad(char *key) { ht_find_new(dict,key)->val = 1; }
  1210. int main(int argc, char *argv[]) {
  1211. FILE *fh;
  1212. int rc;
  1213. /*
  1214. ht_create doesn't handle malloc and calloc failures
  1215. so this is superfluous
  1216. */
  1217. if((dict = ht_create(40000)) == NULL)
  1218. return(fprintf(stderr,"hash creation failed\n"),EXIT_FAILURE);
  1219. if ((fh = fopen("Usr.Dict.Words", "r")) == NULL)
  1220. return(fprintf(stderr,"couldn't open dictionary\n"),EXIT_FAILURE);
  1221. rc = ((handleInput(fh,hashLoad) && handleInput(stdin,spellCheck)) ?
  1222. EXIT_SUCCESS : EXIT_FAILURE);
  1223. ht_destroy(dict);
  1224. return(rc);
  1225. }
  1226. Statistical Moments
  1227. /* -*- mode: c -*-
  1228. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1229. * http://www.bagley.org/~doug/shootout/
  1230. */
  1231. #include <stdio.h>
  1232. #include <stdlib.h>
  1233. #include <math.h>
  1234. #define MAXLINELEN 128
  1235. int
  1236. compare_doubles (const double *a, const double *b) {
  1237. return (int) (*a - *b);
  1238. }
  1239. int
  1240. main() {
  1241. char line[MAXLINELEN];
  1242. int i, n = 0, mid = 0;
  1243. double sum = 0.0;
  1244. double mean = 0.0;
  1245. double average_deviation = 0.0;
  1246. double standard_deviation = 0.0;
  1247. double variance = 0.0;
  1248. double skew = 0.0;
  1249. double kurtosis = 0.0;
  1250. double median = 0.0;
  1251. double deviation = 0.0;
  1252. int array_size = 4096;
  1253. double *nums = (double *)malloc(array_size * sizeof(double));
  1254. while (fgets(line, MAXLINELEN, stdin)) {
  1255. sum += (nums[n++] = atof(line));
  1256. if (n == array_size) {
  1257. array_size *= 2;
  1258. nums = (double *)realloc(nums, array_size * sizeof(double));
  1259. }
  1260. }
  1261. mean = sum/n;
  1262. for (i=0; i<n; i++) {
  1263. deviation = nums[i] - mean;
  1264. average_deviation += fabs(deviation);
  1265. variance += pow(deviation,2);
  1266. skew += pow(deviation,3);
  1267. kurtosis += pow(deviation,4);
  1268. }
  1269. average_deviation /= n;
  1270. variance /= (n - 1);
  1271. standard_deviation = sqrt(variance);
  1272. if (variance) {
  1273. skew /= (n * variance * standard_deviation);
  1274. kurtosis = (kurtosis/(n * variance * variance)) - 3.0;
  1275. }
  1276. qsort(nums, n, sizeof (double),
  1277. (int (*)(const void *, const void *))compare_doubles);
  1278. mid = (n/2);
  1279. median = n % 2 ? nums[mid] : (nums[mid] + nums[mid-1])/2;
  1280. free(nums);
  1281. printf("n: %d\n", n);
  1282. printf("median: %f\n", median);
  1283. printf("mean: %f\n", mean);
  1284. printf("average_deviation: %f\n", average_deviation);
  1285. printf("standard_deviation: %f\n", standard_deviation);
  1286. printf("variance: %f\n", variance);
  1287. printf("skew: %f\n", skew);
  1288. printf("kurtosis: %f\n", kurtosis);
  1289. return(0);
  1290. }
  1291. String Concatenation
  1292. /* -*- mode: c -*-
  1293. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1294. * http://www.bagley.org/~doug/shootout/
  1295. */
  1296. #include <stdio.h>
  1297. #include <stdlib.h>
  1298. #include <string.h>
  1299. #include <unistd.h>
  1300. #define STUFF "hello\n"
  1301. int
  1302. main(int argc, char *argv[]) {
  1303. int n = ((argc == 2) ? atoi(argv[1]) : 1);
  1304. int i, buflen = 32;
  1305. char *strbuf = calloc(sizeof(char), buflen);
  1306. char *strend = strbuf;
  1307. int stufflen = strlen(STUFF);
  1308. if (!strbuf) { perror("calloc strbuf"); exit(1); }
  1309. for (i=0; i<n; i++) {
  1310. if (((strbuf+buflen)-strend) < (stufflen+1)) {
  1311. buflen = 2*buflen;
  1312. strbuf = realloc(strbuf, buflen);
  1313. if (!strbuf) { perror("realloc strbuf"); exit(1); }
  1314. strend = strbuf + strlen(strbuf);
  1315. }
  1316. strcat(strend, STUFF);
  1317. strend += stufflen;
  1318. }
  1319. fprintf(stdout, "%d\n", strlen(strbuf));
  1320. free(strbuf);
  1321. return(0);
  1322. }
  1323. Sum a Column of Integers
  1324. /* -*- mode: c -*-
  1325. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1326. * http://www.bagley.org/~doug/shootout/
  1327. */
  1328. #include <stdio.h>
  1329. #include <stdlib.h>
  1330. #define MAXLINELEN 128
  1331. int
  1332. main() {
  1333. int sum = 0;
  1334. char line[MAXLINELEN];
  1335. while (fgets(line, MAXLINELEN, stdin)) {
  1336. sum += atoi(line);
  1337. }
  1338. printf("%d\n", sum);
  1339. return(0);
  1340. }
  1341. Word Frequency Count
  1342. /* -*- mode: c -*-
  1343. * $Id: bench.c,v 1.1 2002/06/05 20:34:46 michael Exp $
  1344. * http://www.bagley.org/~doug/shootout/
  1345. */
  1346. #include <stdio.h>
  1347. #include <ctype.h>
  1348. #include <malloc.h>
  1349. #include <stdlib.h>
  1350. #include <string.h>
  1351. #include "simple_hash.h"
  1352. typedef int (*comparator)(const void *, const void *);
  1353. int cmp_hash(struct ht_node **a, struct ht_node **b) {
  1354. int val = (*b)->val - (*a)->val;
  1355. return((val == 0) ? strcmp((*b)->key, (*a)->key) : val);
  1356. }
  1357. int main() {
  1358. int bufsize = 80;
  1359. char *buf = (char *)malloc(bufsize + 1);
  1360. char c;
  1361. int i = 0;
  1362. struct ht_ht *ht = ht_create(75000);
  1363. struct ht_node **sort_array, **sort_tmp, *node;
  1364. while ((c = getchar()) > 0) {
  1365. if (isalpha(c)) {
  1366. buf[i++] = tolower(c);
  1367. if (i == bufsize) {
  1368. bufsize *= 2;
  1369. buf = realloc(buf, bufsize + 1);
  1370. }
  1371. } else {
  1372. if (i > 0) {
  1373. buf[i] = '\0';
  1374. ++(ht_find_new(ht, buf)->val);
  1375. i = 0;
  1376. }
  1377. }
  1378. }
  1379. free(buf);
  1380. sort_array = sort_tmp =
  1381. malloc(sizeof(struct ht_node *) * ht_count(ht));
  1382. for (node=ht_first(ht); (*sort_tmp++ = node) != 0; node=ht_next(ht)) ;
  1383. qsort(sort_array, ht_count(ht), sizeof(struct ht_node *),
  1384. (comparator)cmp_hash);
  1385. for (i=0; i<ht_count(ht); i++)
  1386. printf("%7d\t%s\n", ht_val(sort_array[i]), ht_key(sort_array[i]));
  1387. ht_destroy(ht);
  1388. return(0);
  1389. }