Shop OBEX P1 Docs P2 Docs Learn Events
What is the advantage of C+? — Parallax Forums

What is the advantage of C+?

lardomlardom Posts: 1,659
edited 2012-02-21 08:42 in Propeller 1
I still have a lot to learn about spin and I'm slowly learning how to read Pasm so I'm only asking out of curiosity. C++ appears to be a high level language like spin so I'm guessing its execution speed is similar to spin.
The propeller is capable of both parallel processing and assembly speed. Interrupts? In my mind it's like comparing fuel injection to horse shoes.
The prop has to be one of the best robotic controllers. A system where robots get their marching orders from a computer would be reason enough to learn C+. I'd like to understand that better but that's in the distant future.

Comments

  • Martin_HMartin_H Posts: 4,051
    edited 2012-02-10 07:59
    From the computer scientists point of view, there's nothing better about one Turing complete language over another. Programmers have different standards however.

    C++ has been around since the late 80's and boatloads of code is written in it. This resulted in C++ being the parent language of many modern languages (e.g. Java, JavaScript, C#, and Groovy). So C++'s syntax, operators, and organization are consider normal by a generation of programmers. When another language redefines those operators to a different meaning that is a headwind for programmers using that language. Spin's test and assignment operators which on the surface aren't a bad idea give fits to C++ programmers.

    The longevity of C++ has also resulted in many open source projects adopting it. Google an algorithm and odds are you'll find a sample in C++, basically the entire internet is your object exchange. For example GRBL is an open source G code interpreter that runs on the AVR, with gcc you could probably port it to the propeller, but with Spin it would be a re-write.
  • RaymanRayman Posts: 14,844
    edited 2012-02-10 08:57
    I think C is what most people program microcontrollers in and C++ is what most people program computers in.
    I think of C++ as a superset of C with additional features.
    But, as microcontrollers become more powerful it will make more sense to use C++...
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-02-10 09:14
    lardom wrote: »
    C++ appears to be a high level language like spin so I'm guessing its execution speed is similar to spin.

    Except in certain circumstances, C and C++ are not interpreted languages as Spin is, so speed is going to be different. In C/C++, and limiting the discussion to just microcontrollers, your code is compiled into assembly language for the processor. (There are some exceptions to this, such as .net-powered devices, where the controller contains a runtime.)

    C++ provides standard methods of implementing objects. Spin provides for this, too, though the syntax is necessarily different in order to handle things like loading the object into a new cog.

    As noted by the others, probably the biggest "advantage" of C/C++ is that is so widely used, and the legion of coders is in the millions worldwide. It allows an easier shift between working with microcontrollers, mobile devices, personal computers, and so on.

    -- Gordon
  • ericballericball Posts: 774
    edited 2012-02-10 09:23
    C++ objects also implement advanced functionality like inheritance (defining one object as like another object, but with these differences).
  • jazzedjazzed Posts: 11,803
    edited 2012-02-10 10:08
    Spin is like C++ in that it has "objects" (in C++ an object is a form of a class; a Spin object is more like a C++ class).

    C is procedural and does not really have good ways to organize code. Spin gets kudos for it's objects by comparison. C can be forced into object mode because it has pointers, but that ain't very pretty.

    C++ standardizes "C objects" in a reasonable way and adds a whole lot of extra stuff that you only really need for large system scale-ability. There are many C++ advantages that are usually leveraged only in certain environments. The inheritance feature Eric Ball mentions is probably one of the best general language features that comes from C++ (the worst is, well, debatable).

    Not all of C++ features need to be used for an effective program. It is very comfortable implementing the limited but still powerful ideas of Spin. Eric's (ersmith) Spin to C++ translator demonstrates this nicely.

    C# is a nice alternative to C++, but it is limited to certain platforms.

    @Gordon, in Propeller-GCC we can start a COG several ways. One will be very familiar: cognew(pasm, parameters);
  • Mark_TMark_T Posts: 1,981
    edited 2012-02-10 12:50
    Martin_H wrote: »
    From the computer scientists point of view, there's nothing better about one Turing complete language over another. Programmers have different standards however.

    I think you confuse "better" with "is as Turing complete as". Orthogonality, expressiveness, abstraction, referential transparency, declarativeness all count for nothing then? Or was the smiley implied?
  • Martin_HMartin_H Posts: 4,051
    edited 2012-02-10 13:52
    Mark_T wrote: »
    I think you confuse "better" with "is as Turing complete as". Orthogonality, expressiveness, abstraction, referential transparency, declarativeness all count for nothing then? Or was the smiley implied?

    As you say all those things are important, and I would certainly prefer C++ or Spin over this Turing complete esoteric programming language:
    ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
    

    I was trying to point out is that the advantage some languages enjoy (e.g. C++) over other languages (e.g. Spin) are less about what the language can achieve, and more about the programming ecosystem the languages exist within.
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-02-10 14:09
    I wonder if that's the best-known esoteric language just because of the name?
  • pedwardpedward Posts: 1,642
    edited 2012-02-10 15:07
    I've used a variety of languages in my software career. I've written a lot of heavy lifting in C and used C++. The language of choice really has to do with how easily you can express what you need in the amount of space you have. There are many times that PASM is much more concise for an operation than SPIN. There are times that C++ is more concise than C for developing complex systems.

    I found C++ to be useful where a larger part of the heavy lifting is done for you. Specifically, the STL and widget libraries such as QT. I would avoid programming a UI in anything less than a widget library like QT. Similarly, I had the opportunity to write a network monitoring client in C++, the use of the STL and streams was very valuable and made the code much smaller and much less bug prone.

    On the other hand, I wrote a high performance distributed mail server in C and found the last ugly (logic) bug 10 years after it was in production, having delivered uncounted messages.

    For me, expressing what I want to do and doing it without unnecessary complexity is paramount. Unnecessary complexity breeds bugs and leads to code that is difficult to understand and maintain.

    That said, JavaScript has evolved to a point where it's almost a sport to write unreadable code (see jQuery).
  • jazzedjazzed Posts: 11,803
    edited 2012-02-10 15:53
    Martin_H wrote: »
    As you say all those things are important, and I would certainly prefer C++ or Spin over this Turing complete esoteric programming language:
    ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
    

    :) For the BF impaired ....
    1024 x 879 - 118K
  • Martin_HMartin_H Posts: 4,051
    edited 2012-02-10 16:46
    Jazzed, there's a BF interpreter for the propeller? I didn't know that.
  • jazzedjazzed Posts: 11,803
    edited 2012-02-10 18:54
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-02-12 21:51
    Spin is interpreted, that is a(n assembler) program runs the uses the spin program as data, and does what the spin program defines. Because its a twp step process, the spin program is 2 - 10 times slower than directly executing an assembler program.

    C and its derivatives at intended to be a "portable assembler" that is, the allow a person the can write in C to write code on any processor that has C compiler support. Also, one may take advantage of extensive libraries of function that are already written in C, C++, etc (Did I get that right?) "C" really means "a way to get assembler type executables with having to hand code everything in assembler" which is really great.

    The item that can occasionally cause issues is that all processor families are different to some degree or other. This means that not all library functions can run directly on given processor, without some modification and/or clear understanding of what those differences are. The C guys are going to great lengths to get the C material to run as one would expect on the prop. Sometimes this is a challenge as the prop is so very different in some areas, like interrupts. (the prop uses an entire cog instead of an interupt, etc).

    The bottom line is eventually, we have to know the detail of how the prop works in order to use it to its fullest. The C guys are doing a lot of "packaging" that work for us, but as some point we have to start understand what's what and why.

    There are other ways to get very fast execution speed, and use the processor to its fullest. One is writing directly in assembler, which takes years or decades to master. Other tools exist, but they tend to be consider "fringe" by folks that have a different preference.
  • Heater.Heater. Posts: 21,230
    edited 2012-02-12 21:58
    I think you will find Spin is 50 to 100 times slower than assembler.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2012-02-12 23:08
    The major benefit to knowing C/C++ is that they have bascially become the lingua francas of computer programming. You'll immediately expand your horizons to just about every computing platform readily available.
  • lardomlardom Posts: 1,659
    edited 2012-02-13 07:11
    prof_Braino said,
    There are other ways to get very fast execution speed, and use the processor to its fullest. One is writing directly in assembler, which takes years or decades to master
    While I cannot yet write code in PASM my thinking was that once I learned how I would first write and debug in Spin. I would then rewrite it in PASM. I don't know if that idea makes sense
  • BatangBatang Posts: 234
    edited 2012-02-13 07:26
    lardom, (and others)

    If you learn C/C++ then you will be able to write code for various microprocessors not only the prop.

    Also code written in C/C++ for other microprocessors could be used (with some modification) in a prop project.

    Whilst spin has its place go for C/C++.

    Just a thought!
  • RaymanRayman Posts: 14,844
    edited 2012-02-13 08:53
    lardom wrote: »
    While I cannot yet write code in PASM my thinking was that once I learned how I would first write and debug in Spin. I would then rewrite it in PASM. I don't know if that idea makes sense

    I think this is a sound approach. I always do things in SPIN first if possible and then rewrite in assembly...
  • Heater.Heater. Posts: 21,230
    edited 2012-02-13 09:51
    It's a very sound approach. Back in the day we would write out our ideas in a high level pseudo code for which ther was no compiler. But it allowed us to think about problems in a high level way without worrying about the details of asm. Then when the design looked good its time to code for real in asm.
    How much better then to have something like Spin as your pseudo code which you can actually run and test.
  • BatangBatang Posts: 234
    edited 2012-02-13 09:55
    The girl is the programmer who uses Spin
    The motorcycle is the propeller.
    Spin is the training wheels.


    image.jpg
    240 x 192 - 9K
  • evanhevanh Posts: 16,113
    edited 2012-02-13 16:29
    OOPS: GOTOs done in an organised manner. Hehe, just kidding.
  • 4x5n4x5n Posts: 745
    edited 2012-02-13 16:55
    Spin is interpreted, that is a(n assembler) program runs the uses the spin program as data, and does what the spin program defines. Because its a twp step process, the spin program is 2 - 10 times slower than directly executing an assembler program.

    C and its derivatives at intended to be a "portable assembler" that is, the allow a person the can write in C to write code on any processor that has C compiler support. Also, one may take advantage of extensive libraries of function that are already written in C, C++, etc (Did I get that right?) "C" really means "a way to get assembler type executables with having to hand code everything in assembler" which is really great.

    The item that can occasionally cause issues is that all processor families are different to some degree or other. This means that not all library functions can run directly on given processor, without some modification and/or clear understanding of what those differences are. The C guys are going to great lengths to get the C material to run as one would expect on the prop. Sometimes this is a challenge as the prop is so very different in some areas, like interrupts. (the prop uses an entire cog instead of an interupt, etc).

    The bottom line is eventually, we have to know the detail of how the prop works in order to use it to its fullest. The C guys are doing a lot of "packaging" that work for us, but as some point we have to start understand what's what and why.

    There are other ways to get very fast execution speed, and use the processor to its fullest. One is writing directly in assembler, which takes years or decades to master. Other tools exist, but they tend to be consider "fringe" by folks that have a different preference.

    @prof briano (missed you at the chibots meeting yesterday) It is true that C was intended to be and still is a very low level language a very small step above assembly. While working at Lucent I "snuck <SP?> into a lecture given by Dennis Ritchie and he referred to C as a high level assembler. I have to admit that I never questioned the comment.

    If you write a C program that doesn't make use of any library functions and compile it into assembly how efficient C compilers can be! There's a reason why the very vast majority of "embedded" machine control code is written in C!!

    There's a move towards C++ for machine control but I have to admit to not understanding why.
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-02-21 08:42
    lardom wrote: »
    While I cannot yet write code in PASM my thinking was that once I learned how I would first write and debug in Spin. I would then rewrite it in PASM. I don't know if that idea makes sense

    Late to return to the party---

    This is exactly the method to use. Doing this in a forth environment is particularly fast and easy, once one is familiar enough with the processor architecture and forth.

    While C/C++ are the "lingua franca" of C/C++ programmers, do not fall for the line that they are the "only" or the "best" solution. These are simply a couple screw drivers in the tool box, along with the wrenches and files etc. Use a screw drive when you need a screw driver, but DON'T use a hammer when you need a wrench.
Sign In or Register to comment.