Want to reduce the execution time of for loop

My part of the program is given below.

for(brightness=initvalue; brightness<=finalvalue; brightness++)
{
OCR1A = brightness; // For 12Bit resolution
float voltage = (1 - (brightness/256.0))* -1; // For 12Bit resolution
int16_t adc0 = ads.readADC_SingleEnded(0);
float voltage1 = (adc0 * 0.1875) / 1000;
float current = ((voltage1-2)/Rf);//*(-1); //494 earlier

x1 = (150voltage)+178; // 18 pixel added to 160 pixel to shift the graph right side
y1 = (-11
current*1000)+120; // For -10 to +10ma range

if(k > buff_cycle)
{
tft.drawLine(x1, y1, x2, y2, ILI9341_YELLOW);
}
x2=x1, y2=y1;

if(k > buff_cycle)
{
Serial.print(voltage,3);
Serial.print("\t");
Serial.print(voltage1,3);
Serial.print("\t");
Serial.println(current, 6);
}

if(k > buff_cycle)
{
myFile = SD.open(filename, FILE_WRITE);

if(myFile)
{
myFile.print(voltage,3); //value of voltage in sd card
myFile.print("\t");
myFile.print(current,6);
myFile.print("\t");
myFile.println(k);
myFile.close();
}
else
{
Serial.println(“error opening test.txt”);
tft.setCursor(242,120);
tft.println(“Error”);
}
}

In above program, I am using many activities like ADC, DAC, writing data in SD card, TFT writing, serial communication etc.

The above loop takes approx. 140 msec to execute the code but I want to reduce the execution time. I am using arduino mega board.
Please suggest.

I have to admit, I don't quite understand the intention of your code. The loop looks like it is going to increase the brightness, but then it does stuff like measuring voltages and even writing to the sd card? I'd suggest you clean up your dependencies for more readable and understandable code. Also choose better names for your variables. What is k supposed to mean? buff_cycle? x1, x2? You should also extract some functions to keep your lines of code per function low.

  1. Do you really need to measure the voltage on every loop iteration?
  2. Do you really need to write the measured values directly to sd card? Why not buffer them in SRAM and write the buffer to the sd card after the brightness loop has finished?
  3. Why do you use two if statements with the same condition?
  4. Do you really need to update the tft display that often? This is the same as the sd card, as both the tft display and writes to the sd card will take a significant amount of time.

What speed are you running the serial port at?

For better results, post (or attach) the entire sketch.