Stories
Slash Boxes
Comments

SoylentNews is people

posted by LaminatorX on Sunday March 16 2014, @03:28AM   Printer-friendly
from the premature-optimization-is-the-root-of-all-evil dept.

Subsentient writes:

"I've been writing C for quite some time, but I never followed good conventions I'm afraid, and I never payed much attention to the optimization tricks of the higher C programmers. Sure, I use const when I can, I use the pointer methods for manual string copying, I even use register for all the good that does with modern compilers, but now, I'm trying to write a C-string handling library for personal use, but I need speed, and I really don't want to use inline ASM. So, I am wondering, what would other Soylenters do to write efficient, pure, standards-compliant C?"

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2, Interesting) by ld a, b on Sunday March 16 2014, @10:44AM

    by ld a, b (2414) on Sunday March 16 2014, @10:44AM (#17144)

    Sure, I use const when I can, I use the pointer methods for manual string copying, I even use register for all the good that does with modern compilers, but now, I'm trying to write a C-string handling library for personal use

    The above gives no indication that you understand what you are currently doing, let alone what you intend to do.
    My advice would be to learn the basics before jumping into the pilot's cabin.
    Once you know what you are doing with C, google yourself a good string algorithm that matches your intended use patterns and implement that in non-clever C.
    Once that is done, take the code using your string library and profile it. If it's good enough then just use it as is.
    Once you identify the bottleneck is say the raw character copy, you google again for the fastest way of moving stuff around. You replace your old copying code and then see if it works.
    If you start out looking for fast copying hacks, or improvising with algorithms, you'll end up with a slow unusable piece of crap.
    Intended use is the most important part!
    You can see people here raving about immutable strings or whatever, but "appending" stuff to a simple immutable string object can easily bring a system down. Usually, seemingly dumb algorithms have their strong points as well. It's not "What were System.String or strcat(3) designers smoking?" but "What was I smoking when I used that for my log storage?".

    --
    10 little-endian boys went out to dine, a big-endian carp ate one, and then there were -246.
    Starting Score:    1  point
    Moderation   +1  
       Troll=1, Insightful=1, Interesting=1, Total=3
    Extra 'Interesting' Modifier   0  

    Total Score:   2