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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 1:14 pm

karrika wrote: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!
But the OP is asking about Speed!
Assuming that an algorithm can be implemented using C, Python and Linux/BASH, which programming language is best suited in terms of runtime consumption?
and
If the program takes considerable amount of time for execution, then I might not be able to collect proper data.
Clearly C wins but I suspect C++ would be very close.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

gkreidl
Posts: 6222
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 1:25 pm

I call that stupid programming.

Code: Select all

def fibo2(n):
    farr = [1,1]
    for i in range(0,n-1):
        farr.append(farr[i]+farr[i+1])
    return farr[n-1]

t = time.time()
print fibo2(42)
print time.time()-t
On RPi2:

Code: Select all

python fibo.py
267914296
0.000319957733154
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:09 pm

karrika,
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
Quite right. Line count is perhaps the third most important criteria in language selection, after raw speed and making sure the languages name does not start with the letter "P".

Without obfuscating things too much we can rewrite the JS version like so:

Code: Select all

console.log (function fibo(n) {
    return ((n === 1 || n === 2) ?  1 : fibo(n - 1) + fibo(n - 2));
}(42));
And the C version like so:

Code: Select all

#include <stdio.h>

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

int main (int argc, char* argv[]) {
    return (printf ("%d\n", fibo(12)));
}
So we have:
JS = 3 lines
C = 9 lines
Python = 6 lines

Javascript wins!
Memory in C++ is a leaky abstraction .

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:15 pm

Heater wrote: Conclusions:
1) Never use Python.
1) Don't take notice of anyone so opinionated that they say "never use python".

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: 14729
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:33 pm

jahboater,
Clearly C wins but I suspect C++ would be very close.
It's better than that. C++ will generate exactly the same code as C.

At least for the actual fibo() function.
Memory in C++ is a leaky abstraction .

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 2:41 pm

gkreidl wrote:I call that stupid programming.

Code: Select all

def fibo2(n):
    farr = [1,1]
    for i in range(0,n-1):
        farr.append(farr[i]+farr[i+1])
    return farr[n-1]
In C,

Code: Select all

static int
fibo2( const int n )
{
  int farr[n];
  farr[0] = farr[1] = 1;
  for( int i = 0; i < n - 1; ++i )
    farr[i+2] = farr[i] + farr[i+1];
  return farr[n-1];
}
But to compare languages fairly the test cases should all use the same algorithm, which the original examples did.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

Pithagoros
Posts: 580
Joined: Wed Nov 12, 2014 8:16 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:04 pm

I've tried Python and it's a reasonable approach, but I always end up wondering why I'm messing about and go back to C or C++. For me K&R derived languages are like a comfortable old jacket, so familiar that I know I can be productive and not have to spend too much time figuring stuff out.

I don't like to try and answer the question about which language because generically there is no right answer (there are a few wrong answers). It is often a polarising subject.

But for speed, avoid Python.

The one language / runtime system that I really don't get and can't imagine why it is so popular is Java.

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:37 pm

gkreidl,
I call that stupid programming.
It's not stupid programming. It's a benchmark. It's often used measure of the efficiency of making function calls, with a bit of arithmetic thrown in for good measure.

You are right that no one in their mind would calculate Fibonacci numbers that way in a real application.

On the other hand your fibo2 in Python is stupid programming as well, it's down right buggy. It is growing the array farr up to the size of n. If n is big it's going to run out of memory!

Also it includes the timing of printing the result in the reported execution time.

This is better:

Code: Select all

import time

def fibo2(n):
    farr = [1,1]
    for i in range(0,n-1):
        farr.append(farr[i]+farr[i+1])
    return farr[n-1]

for runs in range (0, 9):
    t = time.time()
    f = fibo2(42)
    diff = time.time() - t
    print f
    print diff
With timing like so:

Code: Select all

$ python fibo2.py
267914296
0.000155925750732
267914296
0.000159978866577
267914296
0.000154972076416
267914296
0.000144004821777
267914296
0.000143051147461
267914296
0.000144004821777
267914296
0.000144004821777
267914296
0.000144004821777
267914296
0.000142097473145
of course that is still slow. We can make the JS equivalent:

Code: Select all

function fibo2(n) {
    let farr = [1, 1];
    for (let i = 2; i < n; i++) {
        farr[i] = farr[i - 1] + farr[i - 2];
    }
    return farr[n - 1];
}

for (let runs = 0; runs < 10; runs += 1) {
    let time = process.hrtime();
    let f = fibo2(42);
    let diff = process.hrtime(time);
    console.log(f);
    console.log(diff[0] + diff[1] * 1e-9);
}
With timing like so:
$ node fibo2.js
[/code]
267914296
0.002088786
267914296
0.000287393
267914296
0.00008401
267914296
0.000089843
267914296
0.000079114
267914296
0.00007552000000000001
267914296
0.000079739
267914296
0.00007942600000000001
267914296
0.00007624900000000001
267914296
0.000073905
[/code]
Which is basically twice as fast.

Note how node.js takes a few iterations of fibo2 to "warm up". That's OK our programs generally run for long periods.

Of course the proper way to do a fast fibo is to just keep two variables holding the last two numbers in the sequence. That is another ten times faster in JS, probably Python too. And does not blow up memory consumption.

I leave that as an exercise for the reader :)
Memory in C++ is a leaky abstraction .

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 4:48 pm

@PeterO,
1) Don't take notice of anyone so opinionated that they say "never use python".
Quite so. It's just an opinion, formed after evaluating the evidence. I'm allowed an opinion am I not?

I'm all for people trying these things out for themselves and drawing their own conclusions.

Which was my original response to the OP here.

@Pithagoros,

Yes, never did understand Java's reason for existing.
Memory in C++ is a leaky abstraction .

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 5:50 pm

Heater wrote:jahboater,
Clearly C wins but I suspect C++ would be very close.
It's better than that. C++ will generate exactly the same code as C.

At least for the actual fibo() function.
Right. I am a proponent of modern C++ for a lot of things, and what the OP is describing is likely a good application for it. "Straight C", in my opinion, is important for maintaining legacy C software, computer science studies, and knowledge of it can help in interfacing with the Linux kernel. Applications being started today would be far better off done in C++. It's just better in every way, and it still lets you get as close to the hardware as you need. You don't pay (in terms of runtime resources) for C++ language features that you don't use. If you just need to be close to the hardware you can write low level code using some C++ features like smart pointers, STL algorithms, etc. It *is* a complex language but you don't need to learn it all.

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 6:14 pm

yodermk,

Yes, the recent C++ standards, "modern C++", include a lot of nice features like autoptr and lambdas and closures. When you start writing code with that it does not look much like the same language any more!

It's kind of ironic. In recent years C++ has gained Javascript like features, those lamdas and closures for example. Meanwhile Javascript has gained C++ like features, like "class", in the new 2015 standard.

I haven't really got my head around all those new features yet. Did any one write that "Modern C++ for Begineers" book yet? It would be good to raise a new generation of C++ coders without filling them up with the old crusty ways.
Memory in C++ is a leaky abstraction .

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

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 6:17 pm

Heater wrote: I'm allowed an opinion am I not?
The problem is that not everyone reading your opinion would realise that it is just an opinion, and not some immutable truth to be followed at all costs.
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: 14729
Joined: Tue Jul 17, 2012 3:02 pm

Re: Coding PI : Which programming language to use

Sun Apr 24, 2016 6:38 pm

That is true, PeterO, at least I present evidence, which anyone can reproduce for themselves in a few minutes, as to why I hold such opinions.

People are free to decide that perhaps they don't care about performance so much and therefore my conclusions are of no concern to them.
Memory in C++ is a leaky abstraction .

Return to “General discussion”