Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C programming tricks

Name: Anonymous 2007-09-15 22:06 ID:WCtjqT0N

While programming optimization that makes ones' code completely unreadable is often a bad thing, there are places for it, like that one inner loop of your code that takes up 98% of the program's running time.

What are you tricks for improving performance in C, other than the obvious inline assembly or the like?

Name: Anonymous 2007-09-17 8:42 ID:p/yux4n8

>>79
Moar like:
while (condition1) {
    ...code...
    if (OMG_DISASTROUS_CONDITION) {
        break;
    }
    ...code...
}


simpler, easier to read and less error-prone (due to no condition repeated) than:

while (condition1 && !OMG_DISASTROUS_CONDITION) {
    ...code...
    if (!OMG_DISASTROUS_CONDITION) {
        ...code that's actually at the same conceputal level than the other block but appears indented...
    }
} //}} is alright, but nicer if it can be avoided

Name: Anonymous 2007-09-17 9:04 ID:Heaven

>>80
incorrect.
"literal strings" are arrays of N length.

Name: Anonymous 2007-09-17 9:06 ID:nNPi4cfO

clearly, the EXPERT PROGRAMMER wouldn't use c in the first place.
better to use java, to produce ENTEPRISE LEVEL, fully scalable, reductible, end-user optimized professional applications.

Name: Anonymous 2007-09-17 9:16 ID:Heaven

oh shit the buzzwords

Name: Anonymous 2007-09-17 13:13 ID:9N6k+w8V

>>79

There are some good reasons to use break.  Here's an example from the program I'm working on:

            for( i = 1; i < 2 && CHECK_MVRANGE(bmx, bmy); i++ )
            {
                static const int mod6[8] = {5,0,1,2,3,4,5,0};
                const int odir = mod6[dir+1];
                COST_MV_X3_DIR( hex2[odir+0][0], hex2[odir+0][1],
                                hex2[odir+1][0], hex2[odir+1][1],
                                hex2[odir+2][0], hex2[odir+2][1],
                                costs );
                dir = -2;
                COPY2_IF_LT( bcost, costs[0], dir, odir-1 );
                COPY2_IF_LT( bcost, costs[1], dir, odir   );
                COPY2_IF_LT( bcost, costs[2], dir, odir+1 );
                if( dir == -2 )
                    break;
                bmx += hex2[dir+1][0];
                bmy += hex2[dir+1][1];
            }


Note how some code has to be run *after* the break statement, so it can't as easily be dumped into the for statement.

Name: Anonymous 2007-09-17 13:14 ID:9N6k+w8V

And note that "i < 2" should be "i < some larger number", that was just a copy paste from some code I was testing.

Name: Anonymous 2007-09-17 17:43 ID:2TXRzsFz

>>83

REAL expert programmers would use a common lisp like cmucl or sbcl and write (proclaim (optimize (speed 3)))




Name: Anonymous 2007-09-17 20:23 ID:VO7oeWyD

SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME SCHEME

Name: Anonymous 2007-12-29 20:42

:0

Name: Anonymous 2007-12-29 22:23

>>89
What the fuck is wrong with you, you little shit.

Name: Anonymous 2007-12-29 23:05

>>90
Where the fuck is your question mark?

Name: Anonymous 2007-12-29 23:06

>>14

Why is that?  Is it because adding 8 to (subtracting 8 from, depending on your architecture) your stack pointer is more expensive than adding/subtracting 4?

Name: Anonymous 2007-12-30 0:08

>>91

You don't understand >>90.

Name: Anonymous 2007-12-30 0:11

>>93
Not without proper punctuation I don't.

Name: Anonymous 2007-12-30 1:07

>>92
You're responding to a three-month-old troll.

Name: Anonymous 2007-12-30 3:11

>>95
You're responding to a one-hour-old response to a troll.

Name: Anonymous 2007-12-30 3:51

>>14,92,95,96
I loled

Name: Anonymous 2007-12-30 4:07

>>39
So that's... INVALID Perl code?

Name: Anonymous 2007-12-30 4:49

>>98
valid perl, invalid C.

Name: Anonymous 2007-12-30 5:43

1. Use fprintf ("fast printf") instead of printf.
2. ++i is faster than both i++ and i = i + 1.
3. void main(void) is faster than int main(void) or int main(int, char **) since no value needs to be returned to the OS.
4. Swapping with exclusive-or (a^=b^=a^=b swaps a and b) is faster than using a temporary. This works for all types (including structures), but not on all compilers. Some compilers may also give you a harmless warning.
5. Static storage duration objects are faster than automatic storage duration objects because the CPU doesn't have to set aside storage on the stack every time a function is called. Make your loop indexes global so that you can use them everywhere:
int i;
void func(void) { for (i = 0; i < 10; i++) ; /* ... */ }
void func2(void) { for (i = 0; i < 20; i++) ; /* ... */ }
/* ... */

6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it):
int arr[256];
size_t realsize;
for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
        if (!memset(arr, 0, realsize)) break;
/* now you know that arr actually has realsize / sizeof (int) elements */

If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).

Name: Anonymous 2007-12-30 6:01

>>100
i lold. hard.

Name: Anonymous 2007-12-30 6:07

Also remember the remark, that is "rm", tool in your UNIX shell.

Name: Anonymous 2007-12-30 6:59

are we talking compilation optimization or code execution? because i don't really care how fast the compile is, in my case i've so far only written applications that compile in a few seconds or less

so what tips do you have for optimizing executable code when you compile with gcc and link with ld to the bsd libc?

Name: Anonymous 2007-12-30 7:33

strip -R.data binary

Name: Anonymous 2007-12-30 13:50

>>102
Don't forget to use -fr, which means fold previous remarks you've made with the new ones. Otherwise you'll erase your old comments! For example:

$ rm -fr ~/ This is my home directory

... will attach the remark "This is my home directory" to ~/, leaving any previous remarks intact.

Also, to view remarks you've made previously, try the -v option:

$ rm -v ~/.Xauthority

Name: Anonymous 2007-12-30 14:16

CFLAGS JUST KICKED IN YO

Name: Anonymous 2007-12-30 14:36

>>105
lol'd

Name: Anonymous 2007-12-30 18:27

>>103
try building with gcc -O3 -s -funroll-loops -fomit-frame-pointer -ffast-math -fno-strength-reduce -fno-exceptions

Name: Anonymous 2007-12-31 9:01

Name: Anonymous 2008-01-08 9:03

>>100
6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it):
O HI I UPGRADED UR ALGORITHM!
#include <stdlib.h>
#include <stdio.h>
char* main() {
  char* DATA = malloc(4097);
  printf("%u\n", *(unsigned int*)(DATA - 4));
  return DATA; /* ONLY PUSSEYS FREE THERE MEMORT; LET THE DOS DO IT!!! */
}

Name: Anonymous 2008-01-08 10:56

>>11
But which is faster? Three XOR instructions or three MOV's using a temporary register?

Name: Anonymous 2008-01-08 11:38

>>111
The MOVs, as they have less true dependencies, and possibly won't occupy an ALU.

Name: Anonymous 2008-01-08 11:40

>>112
XORs require less CPU cycles.
fucking sage

Name: Anonymous 2008-01-08 11:46

>>110
ogod i lold'

Name: Anonymous 2008-01-08 12:05

Yeah, C really should have included a swap operator.

Name: Anonymous 2008-01-08 12:15

>>115
It's impossible moron.

Name: Anonymous 2008-01-08 12:19


#define iswap(x,y) (do uintptr_t __x=x,x=y,y=__x; while(0))
#define pswap(p,s) (do void *__p=p,p=s,s=__p; while(0))
#define fswap(f,g) (do long double __f=f,f=g,g=__f; while(0))

Name: Anonymous 2008-01-08 12:20

>>117
That looks NP-complete to me... or kind of like Perl... but I think parsing Perl is NP-complete.

Name: Anonymous 2008-01-08 17:45

>>116
No

Name: Anonymous 2008-01-08 17:54

Condoms full of razors

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List