Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3945022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:58:13+00:00 2026-05-20T00:58:13+00:00

for a college exercise that I’ve already submitted I needed to read a .txt

  • 0

for a college exercise that I’ve already submitted I needed to read a .txt file wich contained a lot of names of images(1 in each line).
Then I needed to open each image as an ascii file, and read their data(images where in ppm format), and do a series of things with them.
The things is, I noticed my program was taking 70% of the time in the reading the data from the file part, instead of in the other calculations that I was doing (finding number of repetitions of each pixel with a hash table, finding diferents pixels beetween 2 images etc..), which I found quite odd to say the least.

This is how the ppm format looks like:

P3 //This value can be ignored when reading the file, because all image will be correctly formatted
4 4
255 //This value can be also ignored, will be always 255.
0  0  0    0  0  0    0  0  0   15  0 15
0  0  0    0 15  7    0  0  0    0  0  0
0  0  0    0  0  0    0 15  7    0  0  0
15  0 15    0  0  0    0  0  0    0  0  0

This is how I was reading the data from the files:

ifstream fdatos;
fdatos.open(argv[1]); //Open file with the name of all the images

const int size = 128;
char file[size]; //Where I'll get the image name

Image *img;
while (fdatos >> file) { //While there's still images anmes left, continue
    ifstream fimagen;
fimagen.open(file);  //Open image file
img = new Image(fimagen); //Create new image object with it's data file
    ………
    //Rest of the calculations whith that image
    ………
delete img; //Delete image object after done 
    fimagen.close(); //Close image file after done
}

fdatos.close();

And inside the image object read the data like this:

const int tallafirma = 100;
char firma[tallafirma];
fich_in >> std::setw(100) >> firma; // Read the P3 part, can be ignored

int maxvalue, numpixels;
fich_in >> height >> width >> maxvalue; // Read the next three values
numpixels = height*width;
datos = new Pixel[numpixels];

int r,g,b; //Don't need to be ints, max value is 256, so an unsigned char would be ok.
for (int i=0; i<numpixels; i++) {
   fich_in >> r >> g >> b;
   datos[i] = Pixel( r, g ,b);
}
//This last part is the slow one, 
//I thing I should be able to read all this data in one single read 
//to buffer or something which would be stored in an array of unsigned chars, 
//and  then I'd only need to to do:
//buffer[0] -> //Pixel 1 - Red data
//buffer[1] -> //Pixel 1 - Green data
//buffer[2] -> //Pixel 1 - Blue data

So, any Ideas? I think I can improve it quite a bit reading all to an array in one single call, I just don’t know how that is done.

Also, is it posible to know how many images will be in the “index file”? Is it posiible to know the number of lines a file has?(because there’s one file name per line..)

Thanks!!

EDIT: This is How I emasure the time.

#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
  struct timeval t;
  struct timezone tzp;
  gettimeofday(&t, &tzp);
  return t.tv_sec + t.tv_usec*1e-6;
}

double start = get_time();
//Everything to be measured here.
double end = get_time();

cout << end-start << endl;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:58:13+00:00Added an answer on May 20, 2026 at 12:58 am

    You’re allocating memory and deleting it in each loop. I don’t think that is good if you so concern about performance.

    So one improvement you can do is: reuse the memory once it is allocated to your program.

    void *memory = malloc(sizeof(Image)); //reusable memory. 
    
    //placement new to construct the object in the already allocated memory!
    img = new (memory) Image(fimagen);
    
    //...
    
    img->~Image(); //calling the destructor
    
    //when you're done free the memory
    free(memory); //use free, as we had used malloc when allocating!
    

    Likewise, you can reuse memory in the Image class, especially at this line:

     datos = new Pixel[numpixels];
    

    And finally, instead of reading RGB into local variables, then copying them into the image data, is not that elegant, so a little improvement can be done here as well,

     //this is yours : temporaries, and copying!
     fich_in >> r >> g >> b;
     datos[i] = Pixel( r, g ,b);
    
     //this is mine : no temporaries, no copying. directly reading into image data!
     fich_in >> datos[i].r >> datos[i].g >> datos[i].b;
    

    Besides these, I don’t think there is much scope to improve your code performance wise!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a practice exercise at my college we have to make a simple room
In college I've had numerous design and UML oriented courses, and I recognize that
I graduated college last year with a degree in Psychology, but I also took
a collegue of mine proposed to me an exercise from an online judge website,
What are the best practices for Design by Contract programming. At college I learned
I've seen a lot of questions and answers on SO about why I should
In an assignment for college it was suggested to use the C readline function
My college is going to start soon, but I want to do something in
I'm having trouble wrapping my mind around how to calculate the normal for a
Alright, so I'm trying out C++ for the first time, as it looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.