| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: optimisation |
-=> Quoting andrew clarke to All <=-
Hi andrew,
ac> Anybody know how to simplify (or more specifically optimise) the code in
ac> the raw_to_mystruct function? I was thinking along the lines of
ac> x->thang = (raw[3] << 8) | (raw[2] << 8) | (raw[1]
<< 8) | raw[0];
ac> but am unsure if that's a correct translation. Thanks.
You need a few more strategically placed parentheses. :) See below.
/*
* T I M E I T
*
* Test several versions of a function to find the optimal one.
*
* Written by M. Stapleton of Graphic Bits
* Modified from a program by Bob Stout.
*
* Tested 04:58:16, Jul 2 1996
*
* This code is Public Domain.
*
*/
#include
#include
#include
#include
#include
#ifdef AMIGA
#define BIG_ENDIAN
#define CLOCKS_PER_SEC 1000000.0
typedef double clock_t;
clock_t clock(void)
{
unsigned int time[2];
timer(time);
return time[0] * CLOCKS_PER_SEC + time[1];
}
#endif
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef struct
{
unsigned long thang;
}
MYSTRUCT;
/* The functions to test */
void raw2struct_AC(unsigned char *raw, MYSTRUCT * x)
{
x->thang = 0L;
x->thang |= raw[3];
x->thang <<= 8;
x->thang |= raw[2];
x->thang <<= 8;
x->thang |= raw[1];
x->thang <<= 8;
x->thang |= raw[0];
}
void raw2struct_PE(unsigned char *raw, MYSTRUCT * x)
{
x->thang = (raw[3] << 24UL) | (raw[2] << 16UL)
| (raw[1] << 8UL) | raw[0];
}
void raw2struct_MS(unsigned char *raw, MYSTRUCT * x)
{
x->thang = ((((raw[3] << 8UL | raw[2]) << 8UL)
| raw[1]) << 8UL) | raw[0];
}
void raw2struct_MS1(unsigned char *raw, MYSTRUCT * x)
{
#ifdef BIG_ENDIAN
unsigned char *inv = (unsigned char *) x;
inv[0] = raw[3];
inv[1] = raw[2];
inv[2] = raw[1];
inv[3] = raw[0];
#else
*x = *((MYSTRUCT *) raw);
#endif
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* A named function */
struct nFunc
{
char *name;
void (*func)(unsigned char *raw, MYSTRUCT * x);
};
#define FUNC(n) {#n, n}
static struct nFunc funcs[] =
{
FUNC(raw2struct_AC),
FUNC(raw2struct_PE),
FUNC(raw2struct_MS),
FUNC(raw2struct_MS1),
};
#define FUNCS (sizeof funcs / sizeof(struct nFunc))
#define ITERS 1000000L
int main(void)
{
clock_t start, stop;
double ct, cmin = DBL_MAX, cmax = 0;
int i, cminix, cmaxix;
long j;
unsigned long n;
unsigned char raw[4] = {1, 3, 7, 15};
MYSTRUCT mystruct;
for (i = 0; i < FUNCS; i++)
{
start = clock();
for (j = n = 0; j < ITERS; j++)
{
funcs[i].func(raw, &mystruct);
n += mystruct.thang; /* A simple checksum */
}
stop = clock();
ct = (stop - start) / (double)CLOCKS_PER_SEC;
if (ct < cmin)
{
cmin = ct;
cminix = i;
}
if (ct > cmax)
{
cmax = ct;
cmaxix = i;
}
printf("%-35s> Time: %7.3f sec.; Checksum: %lu\n",
funcs[i].name, ct, n);
}
printf("\nBest > %s\n", funcs[cminix].name);
printf("Worst > %s\n", funcs[cmaxix].name);
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Program output (under fairly heavy multitasking :) for an Amiga 2000
with 25MHz 68030:
raw2struct_AC > Time: 39.500 sec.; Checksum: 3180724800
raw2struct_PE > Time: 22.080 sec.; Checksum: 3180724800
raw2struct_MS > Time: 15.040 sec.; Checksum: 3180724800
raw2struct_MS1 > Time: 11.360 sec.; Checksum: 3180724800
Best > raw2struct_MS1
Worst > raw2struct_AC
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Have fun!
Michael Stapleton of Graphic Bits.
* AmyBW v2.10 *
... This tagline is encrypted
--- AmyBW v2.10
* Origin: The Three Amigas - better than two (3:713/615)SEEN-BY: 50/99 620/243 623/630 711/401 409 410 413 430 808 809 932 934 SEEN-BY: 712/508 515 713/111 317 601 611 615 618 700 826 888 914 714/906 SEEN-BY: 800/1 @PATH: 713/615 888 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.