我使用此代码实时打开一个录制文件,并使用fseek()和ftell()来获取文件大小:
typedef struct _Recording Recording;
struct _Recording
{
FILE *file;
long filesize;
int progress_id;
...
};
void start_recording(Recording *recording, char* filepath)
{
...
recording->file = fopen(filepath, "rb");
recording->progress_id =
g_timeout_add (500, (GSourceFunc) progress_timeout, recording);
}
gboolean progress_timeout (Recording *recording)
{
if (recording->file != NULL)
{
fseek(recording->file, 0, SEEK_END);
recording->filesize = ftell(recording->file);
}
return TRUE;
}
void stop_recording(Recording *recording)
{
...
if (recording->file)
{
fclose (recording->file);
recording->file = NULL;
}
if (recording->progress_id != 0)
{
g_source_remove (recording->progress_id);
recording->progress_id = 0;
}
}我在一个循环中使用这个函数(500ms)。需要帮助来优化功能,使其更快。
代码的效率。
使用循环函数更新
发布于 2016-03-08 03:13:42
如果您不需要“最大”(questioned)兼容性,那么对于像fstat这样的use operating system specific函数是有意义的。打开一个文件总是有开销的,甚至会一直到最后--操作系统会尝试预测你在做什么,并且可能会开始将文件内容缓存到内存中--在这种情况下,这是不必要的。
发布于 2016-03-08 03:24:22
代码访问文件系统;文件系统和物理磁盘访问的性能将占主导地位。此外,大多数执行的代码将是操作系统和文件系统代码,而不是您的代码,因此对您的代码进行大量的优化可能不会有帮助。
使用低级或特定于操作系统的API而不是stdio可能会带来一些边际收益,比如stat()或fstat() for POSIX,或者Windows中的GetFileSizeEx()。这些方法直接获取大小,而不是使用fseek(),因此可能会更快,但由于给定的原因,可能并不显着。
https://stackoverflow.com/questions/35851792
复制相似问题