/* * Copyright (c) 2026 Roger Seguin * (https://frogzie.duckdns.org) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // $Id: fibonacci.c,v 1.2 2026/02/07 01:54:54 RogerSeguin Exp $ /* gcc -O3 -o cfibonacci -Wall fibonacci.c */ #include #include #include #include #include const clockid_t CLOCK_ID = // CLOCK_PROCESS_CPUTIME_ID CLOCK_TAI ; int usage(char *cmd) { fprintf (stderr, "Usage: %s {--recursive|--loop} number\n", cmd); return (0); } static uint64_t recursiveCalls = 0; uint64_t fibonacciRecursive(uint32_t n) { // ++recursiveCalls; if (n == 0) return (0); if (n == 1) return (1); return (fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2)); } uint64_t fibonacciLoop(uint32_t n) { uint64_t prev2 = 0; uint64_t prev1 = 1; uint64_t r = 0; if (n == 1) return (1); for (int i = 1; i < n; i++) { r = prev2 + prev1; prev2 = prev1; prev1 = r; } return (r); } int displaySequenceRecursive(uint32_t n) { for (int i = 0; i < n; i++) printf ("%ld ", fibonacciRecursive (i)); puts(""); return (0); } int displaySequenceLoop(uint32_t n) { for (int i = 0; i < n; i++) { printf ("%ld ", fibonacciLoop (i)); } puts(""); return (0); } int main(int argc, char** argv) { if (argc < 3) { usage (argv[0]); return (1); } bool recursiveAlgo = false; if (strcmp (argv[1], "--recursive") == 0) recursiveAlgo = true; else if (strcmp (argv[1], "--loop") != 0) { usage(argv[0]); return (1); } uint32_t n; sscanf (argv[2], "%u", &n); struct timespec startTime, endTime; int status; //displaySequenceRecursive(n); //displaySequenceLoop(n); status = clock_gettime(CLOCK_ID, &startTime); if (status == -1) perror ("Start time"); uint64_t r = (recursiveAlgo ? fibonacciRecursive (n) : fibonacciLoop (n)); status = clock_gettime(CLOCK_ID, &endTime); if (status == -1) perror ("End time"); const char *unit = "ns"; double duration = 1000000000 * endTime.tv_sec + endTime.tv_nsec - 1000000000 * startTime.tv_sec - startTime.tv_nsec; if (duration >= 1000) { unit = "us"; duration /= 1000; } if (duration >= 1000) { unit = "ms"; duration /= 1000; } if (duration >= 1000) { unit = "s"; duration /= 1000; } printf ("Fibonacci(%d)=%lu", n, r); if (recursiveCalls) printf("\t%lu recursions", recursiveCalls); printf("\tCPU time: %lf %s\n", (double) duration, unit); return (0); }