fseek high cpu usage?

This is a discussion on fseek high cpu usage? within the C Programming forums, part of the General Programming Boards category; hi there: just a quick question. i have been working on this code which reads a file that contains information ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    23

    fseek high cpu usage?

    hi there:

    just a quick question.

    i have been working on this code which reads a file that contains information about each frame for animation (blm files). my code is controled my a GUI(slider) and then uses fseek() to locate the frame to be read. a bit like a scraching.

    my questions is that, does fseek() eats a lot of cpu power? for example, i tried to cahnge the slider value continusly and the cpu power went stright up.


    many many thanks

    CHUN

    my code looks like this:

    Code:
    static void blinkenlights_goto(t_blinkenlights* x, t_float frame)
    {
     //where the frame comes from the GUI range 0->1
    // where x->framge_pos[] is any array which remembers the position
    // if each frame in the file x->filed
     char *lineread = (char*) getbytes( BL_MAX_LENGTH );
     int current_frame = x->frame_pos[(int)(frame * (x->frame_no-1))];
     int i, n; 
     
     
     if(frame < 0) post("no negtive numbers, please...");
     else
     {
    
     // this is the line that i think is eatting up my cpu!!!!!!
     fseek(x->x_filed, current_frame, SEEK_SET);
     
     
     for(i=0; ;i++)
     {
      
      fgets(lineread, BL_MAX_LENGTH, x->x_filed );
      if ( (lineread[0] == '\n')) break;
      
      for(n=0; n<(strlen(lineread)-1); n++)
      {
      
       if(lineread[n] == '0')blinkenlights_pixoff( x, n+1, i+1);
        else blinkenlights_pixon( x, n+1, i+1);
        
        //if(lineread[n] == '0')blinkenlights_pixoff( x, n+1, i+1);
        //else blinkenlights_pixon( x, n+1, i+1);
      }
      
     }
     
     //fseek( x->x_filed, 0L, SEEK_SET );
     }
     
     if ( lineread ) freebytes( lineread, BL_MAX_LENGTH );
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > char *lineread = (char*) getbytes( BL_MAX_LENGTH );
    char lineread[BL_MAX_LENGTH];
    Is just as good, and there's no need to free it.
    There's also no need for a NULL allocation check (which you omitted).

    Check out my use of RDTSC here

    Calling strlen() in a loop is bad - there was a thread about that just recently.

    My guess is, the performance has little to do with the actual fseek, and more to do with the fgets and pixel bashing going on.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    thanks for your reaply, i will look into it and will be back later

    cheers

    CHUN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  3. Help with high CPU usage
    By 51dunk in forum C Programming
    Replies: 11
    Last Post: 07-23-2008, 02:25 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 02:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21