use fwrite instead of fread twice, fix delay measurement