FWRITE() - 规模效应和对性能计数 [英] fwrite() - effect of size and count on performance

查看:218
本文介绍了FWRITE() - 规模效应和对性能计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有关于两个参数大小和计数的fwrite的目的(很多困惑)。我试图找出这将是更快 -

There seems to be a lot of confusion regarding the purpose of the two arguments 'size' and 'count' in fwrite(). I am trying to figure out which will be faster -

fwrite(source, 1, 50000, destination);

fwrite(source, 50000, 1, destination);

这是在我的code一个重要的决定,因为这命令将被执行的次数以百万计。

This is an important decision in my code as this command will be executed millions of times.

现在,我可以跳到测试,并使用其中一个给了更好的结果,但问题是,code适用于多种平台。

Now, I could just jump to testing and use the one which gives better results, but the problem is that the code is intended for MANY platforms.

因此​​,


  • 我怎样才能得到一个明确的答案,这是跨平台更好?

  • How can I get a definitive answer to which is better across platforms?

FWRITE()的意志实现逻辑从平台而异?

Will implementation logic of fwrite() vary from platform to platform?

我知道有类似的问题(<一个href=\"http://stackoverflow.com/questions/295994/what-is-the-rationale-for-fread-fwrite-taking-size-and-count-as-arguments\">What是FREAD / FWRITE回吐大小的基本原理和计算作为参数?,性能的FWRITE和写入大小),但明白这是关于同一个问题不同的问题。类似的问题的答案,请不要在此情况下,就足够了。

I realize there are similar questions (What is the rationale for fread/fwrite taking size and count as arguments?, Performance of fwrite and write size) but do understand that this is a different question regarding the same issue. The answers in similar questions do not suffice in this case.

推荐答案

性能不应该依赖于任何一种方式,因为任何人实施FWRITE会成倍大小和数量的多少来确定I / O的事情。

The performance should not depend on either way, because anyone implementing fwrite would multiply size and count to determine how much I/O to do.

这是由FreeBSD的libc实现 fwrite.c ,这在其全部读取例证(包括省略指令):

This is exemplified by FreeBSD's libc implementation of fwrite.c, which in its entirety reads (include directives elided):

/*
 * Write `count' objects (each size `size') from memory to the given file.
 * Return the number of whole objects written.
 */
size_t
fwrite(buf, size, count, fp)
    const void * __restrict buf;
    size_t size, count;
    FILE * __restrict fp;
{
    size_t n;
    struct __suio uio;
    struct __siov iov;

    /*
     * ANSI and SUSv2 require a return value of 0 if size or count are 0.
     */
    if ((count == 0) || (size == 0))
        return (0);

    /*
     * Check for integer overflow.  As an optimization, first check that
     * at least one of {count, size} is at least 2^16, since if both
     * values are less than that, their product can't possible overflow
     * (size_t is always at least 32 bits on FreeBSD).
     */
    if (((count | size) > 0xFFFF) &&
        (count > SIZE_MAX / size)) {
        errno = EINVAL;
        fp->_flags |= __SERR;
        return (0);
    }

    n = count * size;

    iov.iov_base = (void *)buf;
    uio.uio_resid = iov.iov_len = n;
    uio.uio_iov = &iov;
    uio.uio_iovcnt = 1;

    FLOCKFILE(fp);
    ORIENT(fp, -1);
    /*
     * The usual case is success (__sfvwrite returns 0);
     * skip the divide if this happens, since divides are
     * generally slow and since this occurs whenever size==0.
     */
    if (__sfvwrite(fp, &uio) != 0)
        count = (n - uio.uio_resid) / size;
    FUNLOCKFILE(fp);
    return (count);
}

这篇关于FWRITE() - 规模效应和对性能计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆