Thread: working, but slow simple graphing routine in gnuplot

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    working, but slow simple graphing routine in gnuplot

    I'm trying to figure out a way to speed up my code, any comments are welcome. Currently I write to file called data.dat and then read from it for plotting, but it is slow.

    This program requires gnuplot or more specifically pgnuplot. You will need to
    change the following line to the location of your pgnuplot within the code in order for the program to launch the plotter.

    fpPlot = _popen("c:\\progra~1\\gnuoct~1.73\\bin\\pgnuplot"

    Code:
    #include <stdio.h>
    #include <windows.h>  // required for "#include <winbase.h>"
    #include <winbase.h>  // for sleep()
    
    #define MAX_NUM 20
    
    int main(void) {
    
      int k;
      FILE *fpPlot,
           *fpWrite;
    
      fpWrite = fopen("data.dat", "a");
      if (fpWrite == NULL) {
        printf("\nCANNOT CREATE OUTPUT FILE\nPROGRAM EXECUTION ABORTED.\n");
        exit(1);
      }
      fprintf(fpWrite, "# Test file for C - gnuplot\n");
      fclose(fpWrite);
    
      fpPlot = _popen("c:\\progra~1\\gnuoct~1.73\\bin\\pgnuplot", "w");
      fprintf(fpPlot, "set xrange[0:%d]\n", MAX_NUM);
      fprintf(fpPlot, "set yrange[0:%d]\n", MAX_NUM);
      fprintf(fpPlot, "set nokey\n");
      for(k=0;k<MAX_NUM+1;k++) {
        fpWrite = fopen("data.dat", "a");
        if (fpWrite == NULL) {
          printf("\nCANNOT CREATE OUTPUT FILE\nPROGRAM EXECUTION ABORTED.\n");
          exit(1);
        }
        fprintf(fpWrite, "%d\n", k);
        fflush(fpWrite);
        fprintf(fpPlot, "plot \"data.dat\" with lines\n");
        fflush(fpPlot);
        Sleep(500);
        fclose(fpWrite);
      }
      Sleep(5000);
      fclose(fpPlot);
      fclose(fpWrite);
      fprintf(fpPlot, "exit");
    return 0;
    }
    Last edited by noguru; 07-26-2006 at 11:29 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm trying to figure out a way to speed up my code
    On top of being I/O bound, you do a lot of sleeping. What part of your program is slow than isn't totally expected?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    I made a bonehead mistake and still was sleeping between each plot point. I did that to ensure I was plotting the correct data and forgot to comment it out again. duh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple macro not working???
    By COBOL2C++ in forum C++ Programming
    Replies: 9
    Last Post: 09-10-2003, 01:43 PM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  4. byte routine not working
    By cppdude in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2002, 10:59 PM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM