GithubHelp home page GithubHelp logo

busfactor1inc / lisp-vs-c-vs-sh Goto Github PK

View Code? Open in Web Editor NEW

This project forked from informatimago/lisp-vs-c

0.0 2.0 0.0 13 KB

A little comparison of a lisp program vs. a C program vs a POSIX UNIX Shell Script program

Makefile 76.78% C 7.86% Common Lisp 15.36%

lisp-vs-c-vs-sh's Introduction

Little Comparison Between Common Lisp and C and Shell Script
============================================================

Here is a little program: compute factorial of 42,
implemented in C and in Common Lisp and in Shell Script.

We note that:

1- the C program runs faster.

2- the C program gives wrong results (who expected differently?).

3- the C program, while being strictly equivalent, counts more
   parentheses, if you count also the braces, the brackets, the
   angle-brackets (of #include), and the semi-colon that surround
   statements…
   
4- Shell Script is measurably slowest.

5- Shell script has the lowest 'syntax' count (parenthesis and other
   syntactical structures).
   

Said otherwise:

1- You cannot feel the difference of run-time between the Lisp program and the C program.

2- the Lisp program gives the correct result.

3- the lisp program is more concise and counts fewer parentheses.

4- the shell script program is most concise and counts even fewer parens.


==== fact.c =======

42! = 0
        0.00 real         0.00 user         0.00 sys

==== fact.lisp ====

42! = 1405006117752879898543142606244511569936384000000000
        0.00 real         0.00 user         0.00 sys

==== fact.unix ====

42! = 1405006117752879898543142606244511569936384000000000
       real    0m      0.172s user     0m0.064s        sys     0m0.120s
       
===================

fact.lisp             24 parentheses, braces, brackets, angle-brackets, semi-colons, commas
fact.c                34 parentheses, braces, brackets, angle-brackets, semi-colons, commas
fact.unix             8 parentheses, braces, brackets, angle-brackets, semi-colons, commas

==== fact.lisp ====

(defun fact (x)
  (if (zerop x)
      1
      (* x (fact (1- x)))))

(defun main ()
  (format t "~%~D! = ~D~%" 42 (fact 42))
  (quit 0))

==== fact.c ====

#include <stdio.h>

int fact(int x)
{
    if(0==x)
    {
        return(1);
    }
    else
    {
        return(x*fact(x-1));
    }
}

int main()
{
   printf("\n%d! = %d\n",42,fact(42));
   return(0);
}

===================

==== fact.unix ====
#!/bin/sh                                                                       

n=42
nf=1
for n in $(seq 1 $n); do
    nf=$(echo "$nf $n * p" | dc)
done
echo $nf


Note: to compare a more complex and real program implemented in Lisp and in C, 
have a look at https://github.com/informatimago/scquery (sources/ and sources-cl/).

scquery in Lisp     1279 parentheses, braces, brackets, angle-brackets, semi-colons, commas
scquery in C        8052 parentheses, braces, brackets, angle-brackets, semi-colons, commas

TODO: write scquery in POSIX UNIX.


lisp-vs-c-vs-sh's People

Contributors

busfactor1inc avatar informatimago avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.