User avatar
bensimmo
Posts: 4182
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 3:16 pm

2.5seconds 'on your pc' means nothing, other than to you.

How does it run on you RaspberryPi (and model of course to compare)?

jalih
Posts: 88
Joined: Mon Apr 15, 2019 3:54 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 3:48 pm

bensimmo wrote:
Sun Jul 14, 2019 3:16 pm
2.5seconds 'on your pc' means nothing, other than to you.

How does it run on you RaspberryPi (and model of course to compare)?
I currently don't own RasperryPI. I got a PINE64 ROCK64 board that I can use for testing, it should give some info about performance. Stay tuned...

jalih
Posts: 88
Joined: Mon Apr 15, 2019 3:54 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 4:54 pm

Michiel O. wrote:
Sun Jul 14, 2019 12:08 pm
It implements the following example: a traffic light alternates between green and blue, but immediately jumps to red if an alarm button is pressed. After a while, the traffic light switches back to alternating green/blue.
I would just use state map with last state and current state combined as map key and function to set new last state and new current state as map value (code in 8th, I don't know Python):

Code: Select all

\ States
0 constant OFF
1 constant GREEN
2 constant BLUE
3 constant RED  \ Alarm

OFF var, last-state
OFF var, current-state


: make-dword  \ lword hword -- dword
  0xffff n:band 16 n:shl swap
  0xffff n:band
  n:bor >s ;


: build-state-map  \ -- m
  m:new
  OFF OFF    make-dword ( OFF last-state   ! GREEN current-state ! ) m:!
  OFF GREEN  make-dword ( GREEN last-state ! BLUE  current-state ! ) m:!
  BLUE GREEN make-dword ( GREEN last-state ! BLUE  current-state ! ) m:!
  GREEN BLUE make-dword ( BLUE last-state  ! GREEN current-state ! ) m:!
  GREEN RED  make-dword ( RED last-state   ! GREEN current-state ! ) m:!
  RED GREEN  make-dword ( GREEN last-state ! BLUE  current-state ! ) m:!
  RED BLUE   make-dword ( BLUE last-state  ! GREEN current-state ! ) m:!
  BLUE RED   make-dword ( RED last-state   ! BLUE current-state !  ) m:! ;


build-state-map var, state-map

[ "OFF", "GREEN", "BLUE", "RED"] var, colors


\ Demo starts here
: app:main
  (
    last-state @ current-state @ make-dword state-map @
    case
    colors @
    current-state @
    caseof
    . cr
  ) 3 times

  current-state @ last-state !
  RED current-state !
  colors @
  current-state @
  caseof
  . cr

  (
    last-state @ current-state @ make-dword state-map @
    case
    colors @
    current-state @
    caseof
    . cr
  ) 2 times

  current-state @ last-state !
  RED current-state !
  colors @
  current-state @
  caseof
  . cr

  (
    last-state @ current-state @ make-dword state-map @
    case
    colors @
    current-state @
    caseof
    . cr
  ) 4 times
  bye ;
  
This outputs:

Code: Select all

GREEN
BLUE
GREEN
RED
GREEN
BLUE
RED
BLUE
GREEN
BLUE
GREEN
Last edited by jalih on Sun Jul 14, 2019 5:05 pm, edited 1 time in total.

User avatar
joan
Posts: 14354
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 5:01 pm

Can't we just all agree that we dislike Ada?

zwieblum
Posts: 21
Joined: Sun Jul 07, 2019 6:55 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 5:09 pm

Ada? Is this thing still in use? ... Hm, I guess COBOL is still used, too ...

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 5:19 pm

Many people are at work forced to "love" and use something often. :idea:
Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

jalih
Posts: 88
Joined: Mon Apr 15, 2019 3:54 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 5:54 pm

jalih wrote:
Sun Jul 14, 2019 3:48 pm
bensimmo wrote:
Sun Jul 14, 2019 3:16 pm
2.5seconds 'on your pc' means nothing, other than to you.

How does it run on you RaspberryPi (and model of course to compare)?
I currently don't own RasperryPI. I got a PINE64 ROCK64 board that I can use for testing, it should give some info about performance. Stay tuned...
I just tested on my ROCK64 board using american-english wordlist and following 8th code:

Code: Select all


m:new var, anamap
a:new var, anakeys


: s:sort \ s -- s
  null s:/
  ' s:cmpi a:sort
  "" a:join ;


: process-words \ word --
  dup /^[a-z]+$/ r:match nip if
    dup
    >r
    s:sort
    anamap @
    over
    m:exists? if
      over m:@ r> a:push rot swap m:!
    else
      swap a:new r> a:push m:!
    then
    drop
  else
    drop
  then ;


: read-and-check-words
  "/usr/share/dict/words"
  f:open-ro
  ' process-words f:eachline
  f:close ;


: len<  \ key  --
  drop ;


: len>=  \ key --
  anakeys @ swap a:push drop ;


: fetch-ana-list \ key array --
  a:len 2 n:cmp
  1 n:+
  nip
  [ ' len< , ' len>= , ' len>= ]
  swap
  caseof ;


: key-val-cmp          \ key1 key2
  anamap @ swap m:@    \ key1 anamap key2val
  0 a:@ nip            \ key1 anamap val2
  swap rot             \ val2 anamap key1
  m:@ nip              \ val2 key1val
  0 a:@ nip            \ val2 val1
  swap                 \ val1 val2
  s:cmp ;


: sort-keys-by-first-word
  anakeys @ ' key-val-cmp a:sort drop ;


: list-words \ ix value --
  anamap @ swap m:@ nip ' s:cmp a:sort
  " " a:join . cr ;


: app:main
  read-and-check-words
  anamap @ ' fetch-ana-list m:each drop
  sort-keys-by-first-word
  anakeys @ ' list-words a:each! drop
  anakeys @ a:len nip "\nAnagrams: %d\n" s:strfmt .
  bye ;
Result is:

Code: Select all

real	0m1,667s
user	0m1,496s
sys	0m0,148s

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 8:58 pm

scruss wrote:
Michiel O. wrote: I did the same, in Python3, using /usr/share/dict/words (2.5M file, 1 second runtime):
• Though you're not doing half the work that my example did: you include proper nouns and possessives and smash everything to lower case: you need to pick out the words that are just lower case alphanumerics.

• Your output isn't sorted.
Actually, I implemented sorting, it's described in https://www.raspberrypi.org/forums/view ... 0#p1499688

Regarding to proper nouns (and person names?), I see the following lines in my /usr/share/dict/words file:

Jane
jane

One could argue that person names like 'Jane' should not be included in the anagrams. I would have to change my program to skip words that start with an uppercase character. That doesn't sound too difficult to me.
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:01 pm

zwieblum wrote:
Sun Jul 14, 2019 5:09 pm
I guess COBOL is still used, too ...
Indeed, you can install COBOL on your Raspberry Pi:

([email protected]) ~ $ apt-cache search cobol

gnucobol - COBOL compiler
libcob1 - COBOL compiler - runtime library
libcob1-dev - COBOL compiler - development files
libcob4 - COBOL compiler - runtime library
libcob4-dev - COBOL compiler - development files
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:08 pm

Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:16 pm

Running a simple COBOL program on the RPi:

$ sudo apt-install gnucobol

Put the following 'hello world' program in 'hello.cob':

Code: Select all

	IDENTIFICATION DIVISION.
	PROGRAM-ID. Hello.
	ENVIRONMENT DIVISION.
	DATA DIVISION.
	PROCEDURE DIVISION.
	DISPLAY "Hello, " WITH NO ADVANCING
	DISPLAY "world!"
	STOP RUN.
Compile it with:

$ cobc -x hello.cob

and run it with:

$ ./hello

Hello, world!

I remember writing a fibonacci program, once in COBOL. I'll see if I can retrieve it from somewhere.
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:21 pm

Java is Cobol 2 and C-- at once! :idea:
Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:25 pm

Found it!

In file fibo.cob:

Code: Select all

	IDENTIFICATION DIVISION.
	PROGRAM-ID. FIBO.
	DATA DIVISION.
	LOCAL-STORAGE SECTION.
	01 REPORTLINE.
	    05 FILLER PIC X(17) VALUE 'Fibonacci number '.
	    05 FIBONACCI-ORDER PIC 9(6).
	    05 FILLER PIC X(4) VALUE ' is '.
            05 RESULT PIC 9(6).

        01 GLOBAL-WORKING-VARIABLES.
	    05 BUCKET-1 PIC 9(6).
	    05 BUCKET-2 PIC 9(6).
	    05 BUCKET-SUM PIC 9(7).
	    05 NEEDED-ITERATIONS PIC 9(6).

	PROCEDURE DIVISION.

	MAIN SECTION.
	PERFORM CALCULATE-FIBONACCI-NUMBER
	    VARYING FIBONACCI-ORDER FROM 2 BY 1
	    UNTIL FIBONACCI-ORDER > 10.
	STOP RUN.

	CALCULATE-FIBONACCI-NUMBER SECTION.
	MOVE 0 TO BUCKET-1.
	MOVE 1 TO BUCKET-2.
	COMPUTE NEEDED-ITERATIONS = FIBONACCI-ORDER - 1.
	PERFORM FIBONACCI-ITERATION NEEDED-ITERATIONS TIMES.
	MOVE BUCKET-SUM TO RESULT.
	DISPLAY REPORTLINE.

	FIBONACCI-ITERATION SECTION.
	ADD BUCKET-1 TO BUCKET-2 GIVING BUCKET-SUM.
	MOVE BUCKET-2 TO BUCKET-1.
	MOVE BUCKET-SUM TO BUCKET-2.
	END PROGRAM FIBO.
Compile and run with:

$ cobc -x fibo.cob
$ ./fibo

Fibonacci number 000002 is 000001
Fibonacci number 000003 is 000002
Fibonacci number 000004 is 000003
Fibonacci number 000005 is 000005
Fibonacci number 000006 is 000008
Fibonacci number 000007 is 000013
Fibonacci number 000008 is 000021
Fibonacci number 000009 is 000034
Fibonacci number 000010 is 000055

☞ Would you like to be a COBOL programmer? I heard you can make good money with it!
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Sun Jul 14, 2019 9:31 pm

If it is programming just Fibbonaci - Hell yeah! :shock: :idea:
Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

Andyroo

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 1:03 am

Michiel O. wrote:
Sun Jul 14, 2019 9:25 pm
...
☞ Would you like to be a COBOL programmer? I heard you can make good money with it!
Been there, done that and no wish to return to it :lol: Rates are not as high as they got up to as a significant amount of code has been converted but some very very large businesses still use it.

England median annual salary (6 months) 2019 -£50,000, 2018 - £44,000, 2017 - £53,500 data from itjobswatch.

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

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 6:12 am

hippy,
Not intending to pick on you, because I seem to see that type of code everywhere. But why that, a While(True) and a Break to escape that loop rather than ...
A very good question...

I started to follow a rule of thumb I recently heard suggested by the ace C programmer and successful game code developer Per Vognsen. Who is, as you might guess, kind of obsessive about efficient code.
https://www.youtube.com/user/pervognsen/featured
https://github.com/pervognsen/bitwise/

His suggestion goes something like this:

"When setting out to create a loop that should perform well start with and endless loop, 'while(1)'"

This seems counter intuitive. So much so that it stuck in my mind.

The reasons he offered for this approach go like this:

1) Your loop is expected to run around many times doing useful work.

2) Testing for the exit condition is not the actual work you want to do and therefore a waste of cycles.

3) So why are you thinking about the exit condition first, rather than the work you want the loop to do?

4) The exit condition may not even be clear to you when setting out. So why not defer thinking about it until you have an optimal solution for the work you really want to do?

5) Thinking about the exit condition first may distract your mind from realizing the optimal solution.

6) When you have something working efficiently then think about changing the loop construct if there a more readable way to present it that does not sacrifice efficiency.

That all seems a bit vague so how about and example: Say you want to write some kind of parser or data stream processing. Your code might look something like this:

Code: Select all

while (1) {
    char c = readInput();

    if (c == 'x') {
        // Do something
        ...
    } else if (c == 'y') {
        // Do something
        ...
    } else if (c == 'z') {
        // Do something
        ...
    } else if (c == 0) {
        // We are done.
        break;
    } else {
        // Do the default thing.
        ...
    }
}
Notice how the code gets straight down to the business of reading the input and processing it. Wasting no programmer thought or CPU cycles on testing the exit condition. There is no time wasted testing the exit condition on every iteration.

In short, starting with and endless loop is an example of doing the common case first, and fast. Exiting is the rare situation so why headline it in your loop construct and waste cycles on it?.

Arguably my example loop you commented on has yet to be progressed to step 6) of the plan above :)

When I heard Per say this a light went on in my head. Why the hell didn't somebody tell me this decades ago. Over the years it would have saved countless hours fretting over what loop construct to use before I even started coding.

Oh no, those dumb asses that teach programming and software construction always said the opposite. Going on about "structured programming" and all that.
Memory in C++ is a leaky abstraction .

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 10:55 am

I bet my ass that ARMers, Embedders and such sort can teach us all some trick about effective design + saving a byte or two in C....
Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

jahboater
Posts: 4762
Joined: Wed Feb 04, 2015 6:38 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 11:53 am

Heater wrote:
Mon Jul 15, 2019 6:12 am
2) Testing for the exit condition is not the actual work you want to do and therefore a waste of cycles.
You can sometimes say "do ....... while( --n != 0 );" and it wont bother to do the test, it will just use the zero flag set by the subtract.

However, in general nowadays, I would rather get the code simple, easy to read, and correct, first, then let the compiler worry about efficiency details.
If anyone looks at the code produced by a recent compiler, especially for loops, you will see it frequently re-arrange things entirely. So it doesn't matter all that much what you put. And the more unnecessarily complicated you make the code, in the naive quest for speed, the less likely the compiler is to work out what you were trying to do and produce something better.
Also, the compiler can optimize loops in hardware specific way - you as the programmer cannot (or should not).

I never use "while(1)" by the way, instead I use "for(;;)" to avoid any messages from static checkers such as "constant in conditional context". The pre-cursor to C (B) had a keyword for this "repeat", so I still use "#define repeat for(;;)" to produce nice readable infinite loops.
[/quote]

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

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:10 pm

jahboater,
However, in general nowadays, I would rather get the code simple, easy to read, and correct, first, then let the compiler worry about efficiency details.
I totally agree. As we must have discussed a few times before I think. I will favor clarity and aesthetics over performance even, if the hit is not significant.

As such, I love the simplicity of while(1) and the way it allows one to write the loop the way one thinks about it, rather than the way the loop syntax and structured programming purists like it.

It's a shame about having to use the clumsy "for(;;)" instead of the clean "while(1)" and then having to hide it in a macro. Blech.
Memory in C++ is a leaky abstraction .

hippy
Posts: 6075
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:33 pm

Heater wrote:
Mon Jul 15, 2019 6:12 am
His suggestion goes something like this: ...
Thanks for providing the explanation.

I'm not sure I agree entirely, but I will admit I have done the following -

Code: Select all

done = False
while not done:
   if ...
   else:
      done = True
which is almost the same, and I'll use "repeat" as the variable and opposite logic when that makes more sense. I guess I just don't like "break' commands and their ilk.

I suppose part of that stems from historically using languages which don't allow it, jumping between languages I'm not entirely familiar with and never being sure of the scope of a 'break' or 'exit' in that language makes one adverse to such things.

I guess we are all somewhat tied down by the baggage of what we are most familiar with and bygone best practice. It has taken me a while to break the 'routines should only have one return point; at its end' rule and use early 'returns' rather than nested if-then-else constructs.
Heater wrote:
Mon Jul 15, 2019 12:10 pm
It's a shame about having to use the clumsy "for(;;)" instead of the clean "while(1)" and then having to hide it in a macro. Blech.
I have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.

User avatar
RaTTuS
Posts: 10484
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:47 pm

How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:48 pm

hippy,
I have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.
Yeah, that is annoying.

We could always us this in C:

Code: Select all

    forever: 
        // ...
        // ...
        goto forever;
Just to annoy people :)
Memory in C++ is a leaky abstraction .

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

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:50 pm

Is everything on twitter so stupid?
Memory in C++ is a leaky abstraction .

User avatar
RaTTuS
Posts: 10484
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 12:55 pm

Heater wrote:
Mon Jul 15, 2019 12:50 pm
Is everything on twitter so stupid?
not everything .....
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

jahboater
Posts: 4762
Joined: Wed Feb 04, 2015 6:38 pm

Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?

Mon Jul 15, 2019 1:55 pm

hippy wrote:
Mon Jul 15, 2019 12:33 pm
I have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.
As I said, the language B did have one

Code: Select all

repeat
{
  ...
}
and you can write exactly the same in C.

(unless you are allergic to pre-processors because Javascript doesn't have one :) ).

Return to “C/C++”