# Thread: Mac OS X Users/C programmers?

1. ## Mac OS X Users/C programmers?

Hi,

I'm buying a laptop and open to using any operating system. Macs being Unix based makes them very appealing to me and I'm trying to convince myself Mac is the way to go. I am used to reading specs for Pentium based machines and don't know how to compare to the processor speeds for Macs. Is a G4 933 MHz going to perform like a Pentium IV 2.4 GHz machine? (They cost about the same.)

To solve this problem I have written a bit of C code (and stolen pieces from the internet too) that I'd like to use as a benchmark for testing various Mac and Wintel machines. I want to be able to walk into a Mac store, pop in a CD and run the benchmark software. Like taking a car for a test drive. My problem is I cannot complile the software for Mac. If someone could compile it on Mac using gcc and email me the Mac executable, gcc version number, and command used to compile (optimizers, etc.) I'd be very grateful. Of course I'll post results once I've done the tests.

I've attached the code below optimistically hoping someone will help me.

Thanks,
Peter

Code:
```#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

#define size 50000000
#define size2 5000000 /*should be greater than number of primes < 2*size+3 */

struct vector {
double *p;
long l; /*length (a.k.a. number of elements)*/
};

void integral(void){
/*based on code from http://www.math.purdue.edu/~dvb/timing.html*/

double sum = 0, delta = 0.025, x = -5;
double y, z;
int i, j,k;
clock_t start;

start=clock();

for(i=0; i <400; i++)
{x += delta;
y = -5;
for(j=0; j<400; j++)
{y += delta;
z=-5;
for(k=0;k<400;k++)
{z += delta;
if(x*x+y*y+z*z<25)
sum += exp(-x*x)*cos(y*y)*cos(z*z);}}}
sum=sum*delta*delta*delta;
/*printf("integral=%f10\n", sum);*/

printf("execution time for integral = %g seconds\n",((double)(clock()-start))/CLOCKS_PER_SEC);
}

void prime (void){
/*based on code from http://www.math.purdue.edu/~dvb/timing.html*/
int i,  p, count=1;
int x, xisprime, sqrtx;
clock_t start;

start=clock();

p = 3;
for(x=3; count < 300000; x+=2)
{ xisprime = 1;
sqrtx = (int) sqrt(x);
for(i=3; i<= sqrtx; i++)
if(x%i == 0)
{xisprime = 0;
break;};
if(xisprime)
{count++;
p=x;};
}
/*printf("the %ith prime is %i\n", count, p);*/

printf("execution time for prime = %g seconds\n",((double)(clock()-start))/CLOCKS_PER_SEC);
}

void goldbach(void){
/*based on code from http://www.math.purdue.edu/~dvb/timing.html*/
int i,j, diff,inc,count=0;
int limit, top,maxp = 3, maxq=3;
char *isprime; /* treated like an integer array */
int *primes;
double x;
int test;
clock_t start;

start=clock();

isprime = (char *)malloc((size_t)(size*sizeof(char)));
limit = (int) sqrt(2*size + 3.5) ;
primes = (int *)malloc((size_t)(size2*sizeof(int)));

if((isprime==NULL)|| (primes == NULL))
{printf("Out of memory\n");
exit(0);}

for(i=0;i<size;i++)
isprime[i]=1;

for(i=0;i<limit; i++)
{inc = 2*i+3;
for(j=3*(i+1); j<size; j+=inc)
isprime[j]=0;}

for(i=0;(i<size)&&(count<size2);i++)
if(isprime[i]){
primes[count] = 2*i+3;
count++;}

top= 2*size;
for(i=6; i <= top ; i+=2)
{test = 0;
for(j=0; primes[j]<i;j++)
{diff = i - primes[j];
if(isprime[(diff-3)/2])
{/*printf("%i = %i + %i\n", i, primes[j], diff);*/
test = 1;
if(primes[j]>=maxp)
{maxp = primes[j];
maxq=diff;}
break;}}
if(test == 0)
{printf("Goldbach fails for %i\n", i);
exit(0);
}}

/*printf("Goldbach true up to %i\n max partition: %i+%i\n", top, maxp, maxq);*/

printf("execution time for Goldbach = %g seconds\n",((double)(clock()-start))/CLOCKS_PER_SEC);
}

void dynamicfnc(void){
struct vector vec;
long requestedlength=3000;
register long i,j;
clock_t start;

start=clock();

for(j=0;j<1000000;j++){
vec.p=(double*)calloc(requestedlength, sizeof(double));
if (vec.p==NULL) {
printf("vec.p==NULL in vec.c/allocvec");
abort();
}
vec.l=requestedlength;

free(vec.p);
vec.l=0;
}
printf("execution time for dynamicfnc = %g seconds\n",((double)(clock()-start))/CLOCKS_PER_SEC );
}

int main(void) {
clock_t start;

start=clock();

integral();
prime();
goldbach();
dynamicfnc();

printf("total execution time = %g seconds\n",((double)(clock()-start))/CLOCKS_PER_SEC );

system("PAUSE");
return 0;
}```

2. ## My system's performance

Results on my desktop

System: Pentium III 450 MHz, 256 MB Ram, Windows XP
Compiler: Dev-C++ 5.0 beta 8 (4.9.8.0) with Mingw/GCC 3.2
Total time to run benchmark: 252 s = 4 min 12 s

3. hint on buying a new comp, buy the new power mac G5 with doubel proccesors it's all you can dream for IMHO...although it's no labtop

4. If you're going to buy a Mac, I would go with the G5 - if you can afford it. But a G4 would be fine too. I don't know If the 933mhz model will perform like a Pentium IV 2.4 GHz, though, but I think it'll give you the power you'll need. Of course it will depend on what you're going to use it for, but for programming, word processing, web, mulimedia and all that it will be plenty.

I compiled your program for you. gcc (GCC) 3.1 20020420 (prerelease), compiled on a G4 733/768/Mac OS X 10.2.8. Compiled with cc bench.c -o bench -Wall (Btw the pause command does not exist..)

5. ## Thank you

Thanks Kristy. I will run that executable on some macs as soon as I can. How did your computer do with the test?

What do you mean the command pause doesn't exist? Did you remove the line of code

Code:
`system("PAUSE");`
If I don't use this line on my PC the console window disappears before I can read the ouput of the program. (Now I'm thinking you are executing from the command line and the output shows up in that terminal and never disappears. It's been a while since I've used command line.)

I want a laptop so no G5 for me.

-Peter

6. Originally posted by petermichaux
(Now I'm thinking you are executing from the command line and the output shows up in that terminal and never disappears. It's been a while since I've used command line.)
Exactly. The system function passes a command to the shell, but there's no command called PAUSE. I didn't remove that line, you'll just get a message at the end of the program from the shell, saying something like 'PAUSE, command not found' (see my test results). I should also mention that I had to comment out two unused variables to make it compile with -Wall If you want me to recompile it or something, let me know, and I will.

My test results:
Code:
```execution time for integral = 27.24 seconds
execution time for prime = 29.14 seconds
execution time for Goldbach = 63.7 seconds
execution time for dynamicfnc = 18.64 seconds
total execution time = 138.72 seconds

7. ## not working

Now I'm at a computer store and I'm trying to run it. I can only save it on the Mac's desktop by draging the icon from the cd to the desktop.

How do I run the file? I double clicked the icon and the Mac asks me which application to use.

If I open a terminal to try the ./bench command to execute I can't even find the right directory.

What's going on here?? Man, I feel useless on another OS.

Peter

8. Open the Terminal and cd to the current directory and run the file with ./bench. If you don't know where you are, drag the bench file to the Terminal, and press enter.

I would first copy the file to the desktop (just drag the icon to the desktop). It's not necessary, but it's faster than running it from a cd.

9. Kristy, thanks again for your help.

I want to buy a Mac but these results aren't swaying me. A \$2000 PC is faster than a \$4300 Mac. And what if that PC was running Linux? Probably faster. Arg. Apple is a frustrating company to like.

Code:
```                            int prime gold dynam total price
Dell PIII 450MHz 256MB      79   44    61   68   252
iMac 800MHz 256MB           25   27    64   17   133

LAPTOPS
iBook G4 800MHz 128MB       18   21    76   12   127   1500
Powerbook G4 1GHz 256MB     26   28    75   18   147   2300
Powerbook G4 1.33GHz 512MB  15   16    43   10    84   4300
HP P4 2.4GHz 512MB          32    8    33    6    80   2000
VAIO P4 2.66GHz 512MB       29    8    33    3    73   2000```

10. Originally posted by kristy
Exactly. The system function passes a command to the shell, but there's no command called PAUSE. I didn't remove that line, you'll just get a message at the end of the program from the shell, saying something like 'PAUSE, command not found' (see my test results).
Hence my wonder at the use of system("PAUSE") when a good ol' getchar() should suffice -- and is in all implementations of C on all platforms.

11. Originally posted by petermichaux
Kristy, thanks again for your help.

I want to buy a Mac but these results aren't swaying me. A \$2000 PC is faster than a \$4300 Mac. And what if that PC was running Linux? Probably faster. Arg. Apple is a frustrating company to like.[/CODE]
It doesn't matter which one runs this particular program faster. First off, what compiler did you use for your mac? Second, what optimization flags did you use for your mac? Third, what compiler did you use for your PC? What optimization flags... see the point?

Depending on what compiler and flags you use, you're going to get varried results. It also depends on what you plan on doing. Some CPUs are better at floating point. Some at ingeters.

Buy a computer that does what you need it to the best.

And why is this in the C forum? Shouldn't this be in the "Help me pick out a computer please!" forum?

Quzah.

12. Originally posted by quzah
It doesn't matter which one runs this particular program faster.
This program has components of things I actually do. It also tests two different things. Also if I'm going to do a test I have to pick some program.

First off, what compiler did you use for your mac? Second, what optimization flags did you use for your mac? Third, what compiler did you use for your PC? What optimization flags... see the point?
gcc on mac with no optimizers. mingw port of gcc on PC with no optimizers

Depending on what compiler and flags you use, you're going to get varried results.
tried to minimize this. These are the compilers I would use on each platform so I think it is a fair test of the ENTIRE system that I'll be using to code.

It also depends on what you plan on doing. Some CPUs are better at floating point. Some at ingeters.
This program has components of things I actually do.
Buy a computer that does what you need it to the best.
Trying to figure that out. Any suggestions I can use to help me?
And why is this in the C forum?
The program is in C and I was looking for a C programmer to help me. I'm new here but it seems to me like a resonable place to look .
Shouldn't this be in the "Help me pick out a computer please!" forum?

13. Just to highlight the difference a compiler makes:
I just ran this test on my AthlonXP1900+ with 256M DDR and WinXP. I first compiled it with MSVC++6. Here are my scores.

Integral= 10.78
Prime= 17.17
Goldbach= 33.03
Dynamicfnc=137.15
Total= 198.52

Then I recompiled it using the DJGPP version of gcc.

Integral= 15.88
Prime= 17.64
Goldbach= 36.43
Dynamicfnc=4.01
Total= 73.96

Same computer, same settings, same programs running. Just two different compilers, both with only default flags on. Interesting results, eh?

14. Very interesting results. Thanks for posting them.

15. I'm pretty surprised myself, I thought they would be a lot closer to each other than they are.

P.S. just to butt my head in...I prefer pc