3
verbis
4y

C or Go? Next language to learn.

Comments
  • 2
  • 3
    Ask Ken Thompson
  • 5
    What do you want to do with the new language?
  • 0
    @Fast-Nop I’m looking something that’s more performant, build extensions, dabble with some API’a and micro services. Perhaps a templating language is my first port of call.
  • 6
    Why not Rust though
  • 8
    @ainsclark That doesn't sound like a good fit for C. You use C if you need to go close to the hardware with only minimal assembly, or if you need shit to be really fast.

    BUT! remember Conan, the secret of steel isn't the sword, it's the hand wielding the sword.

    SImilarly, C isn't fast just because C compilers are awesome (which they are), but because C programmers breathe performance like a tuned muscle car spits fire through the exhaust.
  • 0
    @Fast-Nop thanks 😀

    What example small projects can you think of the top of your head that would require to be closer to the machine level?
  • 2
  • 1
    @FrodoSwaggins

    Let's do something Multi thread.

    Yes your code in C will outperform my C#.

    But it will take me 1 hour instead of 5 days
  • 1
    @ainsclark Either something that is on OS or driver level, or for going bare metal on embedded systems. Both of them require also quite some domain knowledge.

    A nice toy project for making it fast just on application level would be the N-queens-problem for variable board size.
  • 1
    @FrodoSwaggins Aliasing isn't as bad as it sounds because you can always switch it off, or you can move the aliasing into another translation unit and work with void pointers. That's e.g. how to write a fast memcpy.

    And yeah, C++ is good when you would like to use C, but your work is so trivial that you suffer from bore-out and need to spice it up with an insane language.
  • 2
    @NoToJavaScript Threads are easy in C if your problem is either trivial to parallise, or if mutexes won't be a performance bottleneck.
  • 1
    @Fast-Nop Ok here you go. Very trivial problem.

    You have an array of strings containing more than 10.000 ips. You need to ping them all, but not more than 420 at the time.
    (Why would some one want to ping 10.000 ips ? For the same reason some one bought 57 cabbages in your math problem at school)

    You can only use standard libraries.

    Takes 1 line in C#. (Well, 4 with full definition of method and result class)
  • 1
    @NoToJavaScript That's more about standard library functions than multi-threading.

    So to make it an 1:1 comparison, I'd also assume that some C library is available which does the whole pinging stuff as API function. I'm sure there's something on Github already.

    What remains is launching 420 worker threads, which is easy in a loop, implementing a fetch-next-IP function from the array, which works with a mutex so that all threads can just access that with no race condition, and joining the threads.

    There's no way that this would take 5 days.
  • 0
    @Fast-Nop No, it would not take 5 days. My C is rusty as hell (Last time used it was when I was homebrewing some PSP stuff, for fun).
    But I’m pretty sure, myself, I can do the whole exercise in less than a day and I count IDE choice and try/errors.
    But it is so, so easy to write c#, it’s just insane. (And I’m coming from asm, pascal, GWBasic world).
  • 1
    @NoToJavaScript Here's a real life example, just because I had that already floating around from another rant. What it does is factorise a 64 bit integer using N threads in parallel, both handed over als CLI arguments.

    https://pastebin.com/D2bYY5k6

    Took me like 2 hours to bang out, and without the optimised divider part, it would have been considerably less.
  • 1
  • 4
    Rust: A langauge that is universally loved by developers. Get the performance of C and the comfort of a modern high level language. With tight integration with the web technologies you'll be future proof and highly sought-after.
    https://www.rust-lang.org/
    Rust Programming Language
  • 1
    @Fast-Nop

    This is so much code.
    Sorry, but these kind of lines are not for me :

    @highlight
    uint64_t a = *((uint64_t *) ptr_a), b = *((uint64_t *) ptr_b);
  • 0
  • 1
    @NoToJavaScript It would be less code if the 64 bit integer and the number of threads were given in the source text instead of parsing that along with proper error messages, and if the division routine just tested every odd number instead of avoiding divisions both by 2 and 3. But that would be like 25% slower or so.

    Also, the pointer casts could be rewritten like below, and the compiler would generate the same code. But once you're familiar with pointer-heavy code, you won't need that.

    @highlight
    uint64_t *ptr_u64_a = (uint64_t *) ptr_a;
    uint64_t *ptr_u64_b = (uint64_t *) ptr_b;

    uint64_t a = *ptr_u64_a;
    uint64_t b = *ptr_u64_b;
  • 0
  • 1
    @Fast-Nop That I can understand 😊 As I said, my C is like 8 years old of “not using”
  • 0
    To get a job fast, learn Go
    To learn programming and understand how everything works, from operating systems to most programming languages (php is in C, python is in C, Rust & Go were written in C before the bootstrap, ...), learn C.
    Programming in C will make you a better programmer in other languages (Rust too will make you a better dev)
  • 0
    @MagicSowap I don't know if that is low enough actually. Assembly teachers you about memory segments how it all works in from heap to stack and calling conventions. I would start there if your want an understanding. After that you will almost never make common mistakes/inefficiencies involving memory allocation and addressing.

    That is not the goal of the OP however. I think Go will suit his needs. If not Rust is more interesting but C is more relevant.
  • 1
    @Fast-Nop you rang?

    you mad man. look what you've done. you've started the cycle all over again...
  • 2
    Go, I've kinda fallen in love with it and a lot of small stuff can be done really quickly with it
  • 0
    C then Go why not just learn both? They’re both great!
  • 0
    @FrodoSwaggins Aliasing doesn't mean handing pointers around. It means that changing some *float lets the compiler assume that a *int hasn't changed.

    You can turn that off using either e.g. -fno-strict-aliasing (GCC), or bypass it by moving the aliasing into another translation unit with a *void conversion in the function parameters.

    Especially for numerics where everything is *float or *double and therefore can alias, that's a performance problem compared to Fortran where no aliasing is allowed, that's why C99 introduced the "restrict" qualifier.
  • 0
    @FrodoSwaggins Aliasing is about two pointers pointing to the same memory, writing via one of them, and reading via the other. Not about side effects.

    That is guaranteed to work as intended only if the pointer types are compatible. If they are incompatible, like float and int, it's undefined behaviour.

    And that makes sense because it avoids useless reloads, which helps performance. That's also why "restrict", to avoid these useless reloads even when dealing with compatible pointer types.

    However, I agree that it's a bit dangerous because there's not necessarily a warning. I recommend to benchmark whether switching off strict aliasing really slows down the application. If it doesn't, I leave strict aliasing switched off at least for release.
  • 0
    @Fast-Nop @FrodoSwaggins

    I am a bit curious on how you filter / sort data records in C. Talking in memory sorting, not DB related. It’s not to argue “better” or “worse”, but I would be interested to seem some code
    Obviously, there is already a lib which can do equivalent of Array.Sort. But how do you implement, let’s say, sort by TimeStamp descending, then by first name and last name ?
    In c# I currently use about 3 different sorts (Personal p[reference for method 2 as it’s very readable, more than others). The 3d has advantage to NOT create new list object
  • 0
  • 0
    @NoToJavaScript See the example I linked previously, line 253. It uses the comparison function in line 191, which can do anything you like.

    That's how you do it in C if it isn't absolutely performance critical for this part because the comparison function call cannot be inlined. Otherwise, you write your own sorting function.

    C++ doesn't have this issue when using templates, of course.
  • 3
    Go with C
  • 1
    Learn Go brahh
Add Comment