dd17
Posts: 5
Joined: Sat Apr 23, 2016 5:41 pm

Coding PI : Which programming language to use

Sat Apr 23, 2016 6:12 pm

Hi,

Assuming that an algorithm can be implemented using C, Python and Linux/BASH, which programming language is best suited in terms of runtime consumption?

Thanks,
Regards,
Debashish

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 1:55 am

A program written in C will easily consume much less resources and run faster than a program written in bash or python. Is that what you mean?

Of course there are tradeoffs in complexity.

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:31 am

For the sake of curiosity why don't you just try it?

What is it you actually want to do?

Does it really mater if your finished program takes 2% of CPU time in C or 20% in Python?

Do you really want to spend hours crafting a C version of something that can be written in a minute in Python?
Memory in C++ is a leaky abstraction .

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:56 am

dd17 wrote:Assuming that an algorithm can be implemented using C, Python and Linux/BASH ...
The question really doesn't make much sense <sorry> and is generally flame bait (in my experience answering it).

That said, all systems (like people) have a heart language. Here goes--- given that you can say something in English, Spanish, French, Russian, and German which one is ...

... its just a stupid question <sorry, really it is>. What is 'your' heart language--um, your mother tongue--? Mine is English... I can speak out some Russian, some Spanish... and I'll give German a try too... but French and I have a hard time with each other [not a bad attitude, just I don't get it!]. But, my 'heart' language is English.

Computer systems are the same (honestly, they are!) Here is a table of systems and 'their heart' languages:

VM/CMS:---REXX
System36 / System38:---RPG2 / RPG3
Unix:---C
Microsoft:---C# / .NET
Android:---Java
Apple:---Swift
gnu+linux:---C / C++ {gnu suite of tools}

Raspberry PI:---Python

The last one may surprise some of you--- why not C, isn't RPi unix-like, isn't RPi gnu+linux, isn't [it] (debian) written mostly in C?
The Raspberry PI (hardware GPIO and Software together, as an educational tool) is optimized to use Python as it's 'heart' language. Can you use bash, C, C++, Swift, REXX, &c... sure. But if you want to build software on the Raspberry PI that will interface with the outside world--- you will build it most productively (and most easily) in Python; end of story.
Last edited by MarkHaysHarris777 on Sun Apr 24, 2016 4:24 am, edited 1 time in total.
marcus
:ugeek:

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:09 am

Hmm...

In what way is the Raspberry Pi and Raspbian optimized for running Python?

I see no sign of it.
...if you want to build software on the Raspberry PI that will interface with the outside world--- you will build it most productively (and most easily) in Python; end of story.
Sounds very authoritative. I suspect it's just an opinion though.
Memory in C++ is a leaky abstraction .

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:10 am

MarkHaysHarris777 wrote: but French and I have a hard time with each other [not a bad attitude, just I don't get it!].
This is because about half the letters appear to have nothing to do with pronouncing it. And another third appear to be attached to the wrong word! I'm surprised that the French can even figure it out.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:12 am

stderr wrote:
MarkHaysHarris777 wrote: but French and I have a hard time with each other [not a bad attitude, just I don't get it!].
This is because about half the letters appear to have nothing to do with pronouncing it. And another third appear to be attached to the wrong word! I'm surprised that the French can even figure it out.
Yes, precisely... I gave it the good 'ol college try; and um, I didn't do so well.
marcus
:ugeek:

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:13 am

Heater wrote:In what way is the Raspberry Pi and Raspbian optimized for running Python?
I see no sign of it.
It is optimised by having a community of people who think that python is the cat's meow for the pi. Cat's don't much like pie and they hate pythons. Personally, I recommend regular expressions.

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:19 am

Rather like English then.

I mean which letters of "I" and "eye" and "why" and "die" have anything to do with the predominant sound of the words?

You know hat I mean, there are millions of examples.

If you want a language that is consistent in it's spelling and pronunciation you will have to learn Finnish. Good luck getting your head around the grammar though.
Memory in C++ is a leaky abstraction .

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:25 am

stderr wrote:
Heater wrote:In what way is the Raspberry Pi and Raspbian optimized for running Python?
I see no sign of it.
It is optimized by having a community of people who think that python is the cat's meow ...
There is a lot of truth to that statement above :roll: by stderr. I will add, the python libraries for GPIO are the most complete, they are the easiest to use by noobs, they have the MOST examples on-line that actually work ... & I can go on-and-on...

... python is ubiquitous, and python is (and pythonistas hate this, but its true) most BASIC like... more people are able to pick up python from 'scratch' and actually make a real-world interesting project with it WITHOUT having to become professional programmers... just like BASIC in 1964 (Dartmouth College).

And the thing is (just like REXX on VM) the more it is used and found useful, the more it will be used and deemed indispensable; and on it goes...

Cats meow? Yeah babby.....
marcus
:ugeek:

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:33 am

I always though Python was a joke. It's named after a comedy show after all.

It's certainly a lot more complex than good old 60's vintage BASIC.

But I agree, if you have never programmed before Python is probably a good introduction, as BASIC was back in the day.
Memory in C++ is a leaky abstraction .

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:52 am

Javascript shall be the language thou shalt program in, and the language of the programming shall be Javascript. Python shalt thou not program, neither program thou C/C++, excepting thou then proceed to Javascript. Java is right out.
Memory in C++ is a leaky abstraction .

W. H. Heydt
Posts: 12425
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 3:53 am

I'm going to approach the question from a slightly different direction...

It depends on whether you need to run the program once, or if it will be run many times. If it is going to be run a lot, writing it is a language that may be harder or more complex, but which requires less in machine resources makes some sense. If it is a one-off run, then you need to--to the extent you can--optimize your own time so long as the run time doesn't get excessive, so really optimizing your time plus the machine time.

The problem here is that, even if it is the intent, somehow one never winds up using a program just once.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:08 am

Heater wrote:Javascript shall be the language thou shalt program in, and the language of the programming shall be Javascript. Python shalt thou not program, neither program thou C/C++, excepting thou then proceed to Javascript. Java is right out.
Python joke:

"Oh, my program is 50 times slower than C!"

<two weeks working really hard optimising the Python program>

"Oh, my program is only 43 times slower than C now!"

dd17
Posts: 5
Joined: Sat Apr 23, 2016 5:41 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:10 am

Heater wrote:For the sake of curiosity why don't you just try it?

What is it you actually want to do?

Does it really mater if your finished program takes 2% of CPU time in C or 20% in Python?

Do you really want to spend hours crafting a C version of something that can be written in a minute in Python?
I want to capture the ADC converted data from ADS1115 in continuous mode at 860 SPS. If the program takes considerable amount of time for execution, then I might not be able to collect proper data. So I wanted to know which programming language is best suited for such application.

Thanks & Regards,
Debashish

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:45 am

dd17 wrote:I want to capture the ADC converted data from ADS1115 in continuous mode at 860 SPS. If the program takes considerable amount of time for execution, then I might not be able to collect proper data. So I wanted to know which programming language is best suited for such application.
Does your program take a considerable amount of time for execution? The datasheet from TI says that the next sample is started after the last was read meaning that you can read at any rate up to the max rate of the device, see page 10 of http://www.ti.com/lit/ds/symlink/ads1115.pdf . So perhaps do a quick test with python and see what sort of rate you get while watching htop in another terminal.

jahboater
Posts: 5675
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 7:30 am

dd17 wrote: I want to capture the ADC converted data from ADS1115 in continuous mode at 860 SPS. If the program takes considerable amount of time for execution, then I might not be able to collect proper data. So I wanted to know which programming language is best suited for such application.

Thanks & Regards,
Debashish
If you are using the GPIO, personally I would use the pigpio library and do it in C.
http://abyz.co.uk/rpi/pigpio/

Python is great and has huge support on the Pi, but C will always be very much faster.
That's why the OS and most of the software is written in C, indeed more than half of all the software in the world is written in C. The linux kernel in Android is in C. I suspect the innards of Apples OS (which was unix, FreeBSD) is in C.

Many of the Python libraries are written in C.

C has the overhead of having to be compiled once (translated to machine code), Python starts instantly but does the translation every time it is run, or worse, every time round a loop.

You can expect at least 20 times the speed, probably more, by using C.
Pi4 8GB running PIOS64


User avatar
PeterO
Posts: 5828
Joined: Sun Jul 22, 2012 4:14 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 8:40 am

MarkHaysHarris777 wrote:
dd17 wrote:Assuming that an algorithm can be implemented using C, Python and Linux/BASH ...
The question really doesn't make much sense <sorry> and is generally flame bait (in my experience answering it).
The question makes perfect sense, and in fact the first reply gave the correct answer. You've decided to answer a different question as is often the case with your off topic answers.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5828
Joined: Sun Jul 22, 2012 4:14 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 8:42 am

Heater wrote:For the sake of curiosity why don't you just try it?
What is it you actually want to do?
Does it really mater if your finished program takes 2% of CPU time in C or 20% in Python?
Do you really want to spend hours crafting a C version of something that can be written in a minute in Python?
But if it takes 100% in python it's a different answer :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 9:14 am

PeterO,

Actually...Python is never the answer, loathsome language that it is. Unless you happen to want to do arithmetic on 200 digit numbers perhaps.
Memory in C++ is a leaky abstraction .

jahboater
Posts: 5675
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 9:20 am

Heater wrote:Unless you happen to want to do arithmetic on 200 digit numbers perhaps.
You can do that in C too with the GNU libraries GMP or MPFR or MPC (for complex).
Pi4 8GB running PIOS64

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 11:02 am

Indeed you can. It's a lot more complicated to do than just writing the expressions you want in Python.
Memory in C++ is a leaky abstraction .

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 11:31 am

I have here the ultimate benchmark. The recursive calculation of the Nth fibonacci number, where N = 42 in this case. Nothing complicated, just some arithmetical and logical ops and function calls.

This benchmark conclusively proves that Python is a joke, a very sad joke.

Here is the algorithm in C, Javascript, Python, Perl, PHP:

fibo.c

Code: Select all

#include <stdio.h>

int fibo(n) {
    if (n == 1 || n == 2)
    {
        return 1;
    }
    return fibo(n - 1) + fibo(n - 2);
}

int main (int argc, char* argv[])
{
    printf ("%d\n", fibo(42));
    return(0);
}
fibo.js (node.js)

Code: Select all

"use strict";

function fibo(n) {
    if (n === 1 || n === 2) {
        return 1;
    }
    return fibo(n - 1) + fibo(n - 2);
}

console.log (fibo(42));
fibo.py

Code: Select all

def fibo(n):
    if n == 1 or n == 2:
        return 1
    return fibo(n - 1) + fibo(n - 2)

print fibo(42)
fibo.pl

Code: Select all

print fibonacci( 42 ), "\n";

sub fibonacci
{
   my $number = shift;  # get the first argument

   if ( $number == 1 or $number == 2 ) { # base case
      return 1;
   } 

   else {                                # recursive step
      return fibonacci( $number - 1 ) + fibonacci( $number - 2 );
   }
}
fibo.php

Code: Select all

<?php
function fibo ($n)
{
    if ($n == 1 || $n == 2)
    {
        return 1;
    }
    else
    {
        return fibo($n - 1) + fibo($n - 2);
    }
}
echo fibo(42) + "\n";
?>
Here are the execution times in seconds and relative execution time on my Pi 2:

Code: Select all

C           7       1
Javascript  49      7
Python      1225    175
PHP         1482    212
Perl        5885    840
Python is 25 times slower than JS and 175 times slower than C. The only thing that saves it's face is that PHP and Perl are even worse!

Conclusions:

1) Never use Python.
2) In fact, avoid all languages whose name starts with a "P" :)
3) Write your program in Javascript, it's much easier than messing with C.
4) If it looks like JS may turn out to be slow rewrite the thing in C/C++.

Notes:
3) above can be very easy if you have written your JS in a C like manner. That is not using too many funky JS features. And used pigpio which is also available for JS
The exception to 1) above is if you want to do maths on 200 digit numbers which Python handles with ease.
Memory in C++ is a leaky abstraction .

User avatar
karrika
Posts: 1123
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 11:46 am

Speed may not be the only thing that matters:

C = 15 lines of code
JS = 10 lines of code
Python = 6 lines of code
Perl = 14 lines of code
PHP = 14 lines of code

Python wins!

Return to “General discussion”