Thread: Optimise?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Optimise?

    Hi there, newbie to the forum but long time C-programmer, although not 100% up to it. Haha!!! I program primarily to do data manipulation, stuff that can really be done in MathCAD or excel, but lacks the cool factor. Tend to work with large arrays of numbers. Hence, I've typically been using arrays most of the time.

    Recently, my new job has moved me to a windows platform, and I've downloaded the lcc-win32 compiler. Now, when my arrays are smallish, the program runs fine... but if I bring it up to say a[10000], it blows up - program doesnt run.

    Can anyone provide a hindsight on how I can optimize the program... or maybe how to increase allocated memory?

    Snippets of my code is below: (Yes, I do need that many variables!!)

    Its all set at [100] which runs, but if I increase to [10000] (which I need now!), it doesnt!

    Thanks in advance!!!

    Thom

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void title(void);
    
    int main(int argv, char *argc[])
    {
    	int i=0, j=0, ch=0;
        int count=1;
    	float kp[100], ethermal[100], nt[100], e_sp1[100], e_sp2[100], e_sp3[100], e_sp4[100], fy[100];
    	float Pe, d_t, f2, sigma_hoop, EcO, EcI[100], gfunct, Pyc[100], PE1, PE, PC1[100], PC[100];
    	float e_sp1_net[100], e_sp2_net[100], e_sp3_net[100], e_sp4_net[100], e_average_net[100];
    	float e_bend1_net[100], e_bend2_net[100], e_bend3_net[100], e_bend4_net[100];
    	float e_res1[100], e_resultant[100], UR1[100], UR2[100], UR3[100], UR[100];
    	float params[16];
    
    	float grav, wd, T_amb, rho_sea, D, WT, CRA, SMYS, SMTS, E, nu, Pi, fluid, f0;
    
    	FILE *fp;
    	FILE *fp2;
    
    /* Usage Error trapping */
    
        if(argv !=3)
        	{
    			title();
    			printf("ERROR!!! - Proper usage is:\n");
    			printf("\n %s input_filename output_filename\n", argc[0]);
    			printf("... where input_filename = File to be post-processed\n");
    			printf("     and output_filename = File for program output\n");
    			return(1);
    		}
    
    /* Data Checking */
    
    	if ((fp=fopen(argc[1], "r")) == NULL )
    		{
    			title();
    			printf("Error in Opening File: %s \n\n File is Empty\n\n", argc[1]);
    			exit(1);
    		}
    
    	while( (ch=fgetc(fp)) != EOF)
    	{
    		if(ch == '\n')
    			count++;
    	}
    
    /* Input Files */
    
    	fp=fopen(argc[1],"r");
    		for(i=1; i<=count; i++)
    			fscanf(fp,"%f %f %f %f %f %f %f\n", &kp[i], &ethermal[i], &nt[i], &e_sp1[i], &e_sp2[i], &e_sp3[i], &e_sp4[i] );
    
    	fclose(fp);
    
    	fp2=fopen("input_params.dat","r");
    		for(j=1; j<=15; j++)
    			fscanf(fp2, "%f\n", &params[j]);
    	fclose(fp2);
    
    /* Variables Definition */
    
    -
    -
    -
    -
    -
    
    for(i=1; i<=count; i++)
    {
    fprintf(fp2,"%.2f,%.2f,%.2f,%.4e,%.4e,%.4e,%.4e,",kp[i],ethermal[i],nt[i],e_sp1[i],e_sp2[i],e_sp3[i],e_sp4[i]);
    fprintf(fp2,"%.4e,%.4e,%.4e,%.4e,%.4e,%.4e,%.4e\n",e_bend1_net[i],e_bend2_net[i],e_bend3_net[i],e_bend4_net[i],e_average_net[i],e_resultant[i],UR[i]);
    }
    fclose(fp2);
    
    	printf("\n\n === The output file %s has been written...program EXITING === \n\n", argc[2]);
    
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    possibly - stack overflow
    use malloc and allocate the memory dynamically
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Thanks vart, but sorry, no experience in malloc.
    Does it mean I cant use arrays and need to recast in pointers? (which btw, I'm hopeless in!)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could alternatively declare your arrays as static, as in
    static int large[10000];

    Saves all that messing about with malloc (for now)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
     double * array = malloc(100 * sizeof *array);
     if(array)
     {
      int index;
      for(index =0; index <100; index++)
      {
       array[index] = 1/(index + 1.0);
       printf("%.6f,",array[index]);
      }
      free(array);
     }
     return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("\n %s input_filename output_filename\n", argc[0]);
    You meant to use argv, not argc, I imagine. (If your compiler doesn't warn you about this, turn up your warning level.)
    Code:
    printf("\n\n === The output file %s has been written...program EXITING === \n\n", argc[2]);
    Same here . . . .
    Code:
    fp=fopen(argc[1],"r");
    Same here . . . . argc isn't an array, it's the number of elements in the array argv[].

    Rather than opening argv[1] twice (without fclose()ing in between!), you could use the rewind() function.

    You could probably use a two-dimensional array here.
    Code:
    float e_res1[100], e_resultant[100], UR1[100], UR2[100], UR3[100], UR[100];
    I hope you fopen() fp2 again in the section of code snipped, because otherwise you're calling fclose(fp2) twice.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    hes got it right dwks
    Code:
    int main(int argv, char *argc[])
    argc is the array

    can call them anything really

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    *slaps head* Oops.

    Well, don't call them that, you'll confuse every programmer who reads it . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Wow! Thanks guys! Will try the static approach first (completely forgot about that!!!)...
    vart, what does that piece of code do? I dont have my compiler with me now to try!

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    dws, errr.... been using that approach for years without complains!

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wow! I'm amazed. The standard is int argc (count of arguments) and argv (argument vector). You should use it. You could call it count and array I suppose, but switching them is very confusing, because I assume that it is what I consider "normal" . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788

    Cool

    Quote Originally Posted by rtan3005
    vart, what does that piece of code do? I dont have my compiler with me now to try!
    Are you sure, that you are "long time C-programmer"?

    This piece of code shows how to use dynamically allocated array of 100 doubles.

    the code inside if block has no difference from the usage of the static array... That what I tried to illustrate with this sample
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >dws, errr.... been using that approach for years without complains!
    Yes, but how many people have had to maintain your code? I can understand using other names, like count and list, or ac and av, but swapping argc and argv just seems too much like you're screwing with us.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by vart
    Are you sure, that you are "long time C-programmer"?

    This piece of code shows how to use dynamically allocated array of 100 doubles.

    the code inside if block has no difference from the usage of the static array... That what I tried to illustrate with this sample
    Yes I know... hence the smiley. I've heard horror stories from friends of how some aggressive people live and thrive on this board, and was just testing water!

    Cool down guys! Peace!

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by Prelude
    >dws, errr.... been using that approach for years without complains!
    Yes, but how many people have had to maintain your code? I can understand using other names, like count and list, or ac and av, but swapping argc and argv just seems too much like you're screwing with us.
    Nobody!!
    And I do have a bad habit of messing variable names up where they shouldnt be.. sorry!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me optimise my code!
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 10-15-2008, 11:33 AM
  2. Will 'static' optimise my code?
    By samGwilliam in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2005, 11:28 AM
  3. optimise algorithm
    By cyberCLoWn in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2005, 02:27 PM
  4. Optimise FindFiles.
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 01-14-2004, 02:41 AM
  5. How to optimise this c program?
    By megablue in forum C Programming
    Replies: 5
    Last Post: 06-28-2003, 04:20 AM