Efficient code for sd card speed

Other things to look at:

  1. You appear to have a debounced button press test in the loop(). Try eliminating that to see if that has an impact on the performance.

  2. You appear to open and close the SD card file for every write operation. Maybe batch these and close and reopen the file periodically to minimise the risk of data loss.