Slow program/delay

Hi So I am reading ints from a text file on a SD card into an array, text file is literally
array1.txt and its contents are 1,2,3,4,5,6,7

I then create an array of size 7 and dump the content into there, it works fine but there seems to be some delay, which gets alot worse if the array has 20+ numbers. My code is

while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }                                                                     //Making sure Serial comms are still open to help with debugging


  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");                               //Checking that the SD card is initalized, 10 is the pin for you

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
 File myfile = SD.open(file);   
                                                                    //Pick your file to open
 int k=0;                                                         //Counter to get length of the array
 Serial.println(millis());
 while(myfile.parseInt()){
  k++;
  }                                                             //Counting the length of the array
  Serial.println(millis());
   myfile.close(); 
   Serial.println(millis());
   //Close the file again
Serial.println(k);                                              //Debug print the length of the array                                           
Serial.println(millis());
int A[k];                                                       //Declare an empty array of this size
Serial.println(millis());
myfile = SD.open(file);                                         //Opening the file again to read in the array    
  if (myfile)
  {
    while (myfile.available())
    { 
      for(int j=0;j<k;j++)
        {
          A[j]=myfile.parseInt();
        }
      }
      myfile.close(); 
    }                                                                 //A loop that reads the open file, parses it for INTS and uses these to populate the large empty array A
  
  else {
    Serial.println("Cannot open file!");
  }                                                                   //IF something went wrong with opening the file

  //print out array to serial monitor to check it worked
  
    for(int j=0;j<k;j++)
    {
       Serial.println(A[j]);
    }                                                                 //Debug, have the serial print what the populated list is now, hopefully it now has the contents of the array just read in

While the serial output is:

1
array1.txt......array it picks
Initializing SD card...initialization done.
103 ....time via millis
2107....big jump in time.....this is where the delay is, but why?
2107
7...........size of array
2107
2107
1...........printing back the array
2
3
4
5
6
7

From the parseInt() reference page

Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is read;

What is the timeout set to, or is it using the default ?

setting timeout

UKHeliBob:
From the parseInt() reference page

What is the timeout set to, or is it using the default ?

setting timeout

I assume it's using the default, as I never altered it - honestly was not sure how to go about that.

Try making it shorter

  myfile.setTimeout(0);

I believe the problem is that parseInt() uses timedPeek(), and a code of -1 is used to indicate both end of file and no character available. So when the call to read() in SdFile returns -1 for end of file, timedPeek keeps retrying.

MarkT:

  myfile.setTimeout(0);

I believe the problem is that parseInt() uses timedPeek(), and a code of -1 is used to indicate both end of file and no character available. So when the call to read() in SdFile returns -1 for end of file, timedPeek keeps retrying.

Just after trying that, was not sure where to place it, so tried putting it one at a time before every line/loop. No change in anything. The time delay is here according to millis()
...
File myfile = SD.open(file);
//Pick your file to open
int k=0; //Counter to get length of the array
Serial.println(millis()); --- returns 103
myFile.setTimeout(0);
while(myfile.parseInt()){
k++;
} //Counting the length of the array
Serial.println(millis()); --- returns 2103

A follow up again, so I was willing to live with some delay in the program, so I tried it again a on text file with 60 values, eg 1,2,3,4,5,6....

This time it took way longer, and parseInt returned a '0' for everything, odd Im guessing something is timing out?

Is there a better way than parseInt - it was perfect for what i wanted.

Is there a better way than parseInt

You could write your own function. How much control do you have over the format of the data being received ?

Take a look at Serial input basics - updated for ideas

UKHeliBob:
You could write your own function. How much control do you have over the format of the data being received ?

Take a look at Serial input basics - updated for ideas

Total control, but it just text files as lists 1,2,3,4

By write my own function, can you give an example not really sure what to read in.

Future follow up, so my program gets a random number and uses that to pick the file from the SD to read. To make things easier I took out the random number part and told it which file to look at, and to make sure it was not a size issue I had that file contain 70 numbers (1,2,3,4,5,6,7,1,2,3,4.....)

Works perfect and works fast.

So was the random file thing an issue?
int j=random(1,2); //Generate the random number
String file1="array";
String file2=".txt";
String file=file1+j+file2;
String named_seq=file1+j+file2; //making the random file name to be opened later

int j=random(1,2);                                                      //Generate the random number

Generate a random number between 1 and 1. Nothing random about that!

By write my own function, can you give an example not really sure what to read in.

The excellent topic that I linked to has examples