哪个更快? [英] Which is faster?

查看:48
本文介绍了哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用空格填充多个字符字段,或者根据字段填写零'

。哪种方法更快。


char var [10] =" " ;;

var [9] =''\ 0'';





char var [10];

sprintf(var,"");





char var [10];

strcpy(var,"");

var [9] =''\''';




int i;

char var [10];

for(i = 0; i< ; 10; i ++)

{

var [i] ='''';

}

var [i] =''\ 0'';


感谢您的帮助。


贾斯汀

I need to fill a number of character fields with spaces or zero''s
depending on the field. Which method is faster.

char var[10] = " ";
var[9] = ''\0'';

or

char var[10];
sprintf( var, " " );

or

char var[10];
strcpy( var, " " );
var[9] = ''\0'';

or

int i;
char var[10];
for(i=0; i<10; i++)
{
var[i] = '' '';
}
var[i] = ''\0'';

Thanks for the help.

Justin

推荐答案

Justin Robbs写道:
Justin Robbs wrote:
我需要根据字段填充多个带空格或零'的字符字段。哪种方法更快。

char var [10] =" ;
var [9] =''\''';
我认为这个是最快的,可能会被''$ store''指令翻译成

编译器。或者

char var [10];
sprintf(var,"");
涉及函数调用,推送到堆栈,更改程序计数器,

来回传递。或者

char var [10];
strcpy(var,"");
var [9] =''\''';
与上述相同或

int i;
char var [10];
for(i = 0; i< 10; i ++)
{
var [i] ='''';
}
var [i] =''\''';
比函数调用更快,整个代码片段可能会在处理器缓存中保存,我会说这个可以与

相媲美。通过这种方式,您将拥有大量非常快速执行的指令

,具有最少的处理器内存事务(当然不会考虑从缓存中获取内存写入的
帐户) ),与第一种情况下可能只有一个b $ b'多重商店'相比。但是,我个人认为laster

会是最快的。感谢您的帮助。
I need to fill a number of character fields with spaces or zero''s
depending on the field. Which method is faster.

char var[10] = " ";
var[9] = ''\0''; I would think this one is the fastest, will probably be translated by the
compiler by a ''multiple store'' instruction. or

char var[10];
sprintf( var, " " ); Involves a function call, pushing to the stack, changing program counter,
going back and forth. or

char var[10];
strcpy( var, " " );
var[9] = ''\0''; same as above or

int i;
char var[10];
for(i=0; i<10; i++)
{
var[i] = '' '';
}
var[i] = ''\0''; Faster than the function calls, the entire code fragment will be probably
held in the processor cache, I would say this one could be comparable to the
first. This way you will have lots of ''very fast executing'' instructions
with minimal processor-memory transactions (of course not taking into
account the memory writeback from the cache), compared to probably a single
''multiple store'' in the first case. However, I personally think the laster
would be the fastest Thanks for the help.




想一想;


int i = 0;

char tmp ='''';

while(i< 10){

var [i ++] = tmp;

}


我认为这将是最快的,所有内容都将缓存在

处理器缓存,包括变量。


Ahmed



Think about that too;

int i =0;
char tmp= '' '';
while (i < 10) {
var[i++] = tmp;
}

I would think this would be the fastest, everything will be cached on the
processors cache, including the variables.

Ahmed


Justin Robbs< ju *** *********@spamhotmail.com>这样说:
Justin Robbs <ju************@spamhotmail.com> spoke thus:
我需要根据字段用空格或零'来填充多个字符字段。哪种方法更快。


怎么样


char var [10];

memset(var,'''',sizeof var - 1);

var [9] = 0;


?我并不推荐这种方法,但是:)

char var [10] =" ;
var [9] =''\''';


我会说这是你速度最好的选择,因为所有工作都可以在编译时完成。

char var [10];
sprintf(var,"");
char var [10];
strcpy(var,"");
var [9] =''\''';


strcpy附加一个空终止符(假设目标字符串是足够大的
),所以你不需要明确地这样做。

int i;
char var [10];
for(i = 0; i< 10; i ++)
{
var [i] ='' '';
}
var [i] =''\ 0'';
I need to fill a number of character fields with spaces or zero''s
depending on the field. Which method is faster.
How about

char var[10];
memset( var, '' '', sizeof var - 1 );
var[9]=0;

? I don''t recommend that method, however :)
char var[10] = " ";
var[9] = ''\0'';
I''d say this is your best bet for speed, since all the work can be
accomplished at compile time.
char var[10];
sprintf( var, " " ); char var[10];
strcpy( var, " " );
var[9] = ''\0'';
strcpy appends a null terminator (assuming the destination string is
large enough), so you don''t need to do that explicitly.
int i;
char var[10];
for(i=0; i<10; i++)
{
var[i] = '' '';
}
var[i] = ''\0'';




这可能是最慢的,因为库例程也许能够使用特殊的实现功能来加速操作。不过,我的b $ b可能是错的。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



This is probably slowest, since the library routines may be able to
use special implementation features to speed up the operation. I
could be wrong on that, however.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


文章< c6 ********** @ news.tdl.com>,

贾斯汀Robbs< ju ************ @ SPAMhotmail.com>写道:
In article <c6**********@news.tdl.com>,
Justin Robbs <ju************@SPAMhotmail.com> wrote:
我需要根据字段填充多个带空格或零'的字符字段。哪种方法更快。


基准测试系统的各种方式并自行查找。

此外,速度真的是一个很大的问题吗?


但是,这里有一些关于你的每个方法的评论。

char var [10] =" ;
var [9] =''\''';


此方法仅在您声明变量时有效。试图

以下代码

var =" ;

将无效。鉴于这种限制,分配

var [9] =''\ 0'';

是无用的,因为该字符串已经被NUL终止。
< br var> [10];
sprintf(var,"");
我怀疑这是最糟糕的,因为printf和家庭有很多开销。




char var [10];
strcpy(var,"");
var [9] =''\''';
我怀疑这将是速度列表的顶部,但

var [9] =''\''';没用。摆脱它。



int i;
char var [10];
for(i = 0; i< 10; i ++ )
{
var [i] ='''';
}
var [i] =''\ 0'';
I need to fill a number of character fields with spaces or zero''s
depending on the field. Which method is faster.
Benchmark the various ways on your system and find out for yourself.
Also, is speed really that much of an issue?

But, here are some comments on each of your methods.
char var[10] = " ";
var[9] = ''\0'';
This method will only work at the time you declare the variable. Attempting
the following code
var = " ";
will not work. Given that limitation, the assignment
var[9] = ''\0'';
is useless because the string is already NUL terminated.

or

char var[10];
sprintf( var, " " ); I suspect that this is the worst of the lot since printf and family have
a far amount of overhead.


or

char var[10];
strcpy( var, " " );
var[9] = ''\0''; I suspect that this would be towards the top of the list in speed, but
that var[9] = ''\0''; is useless. Get rid of it.

or

int i;
char var[10];
for(i=0; i<10; i++)
{
var[i] = '' '';
}
var[i] = ''\0'';




Ick。这个不符合你的期望。它包含一个缓冲区溢出。

但如果你修复它,它的性能可能与使用strcpy()相当。

唯一的问题是a的开销函数调用strcpy()和如果

供应商提供的库使用机器特定的技巧来获得速度。


但是,我会重复一句老话关于编程。


过早优化是所有邪恶的根源。

Tony Hoare,由Donald Knuth重述。


仔细考虑一下这个字符串的赋值是否真的与

的性能条款有关。它很可能不会。为你的代码挑选好的algorythms

,然后尽可能清楚地表达好的algorythms

。举一个简单的例子,想象一下

2种algorythms的相对表现。


程序一。

使用冒泡排序为它的主要算法是尽可能优化




程序二。

使用堆排序作为其主要算法并且没有太多关注

用于减少CPU周期。


对于非常少量的数据,程序一可能更快,但对于更大的

数据量,程序二将比程序一快得多,它不是很有趣。
甚至都不好笑。一个O(N log N)algorythm与O(N ^ 2)algorythm总计

沼泽任何效果优化可能有。


使用strcpy()并简单地使您的程序可读。



Ick. This one doesn''t do what you expect. It contains a buffer overflow.
But if you fix it, it''s performance may be comparable to using strcpy().
The only question is the overhead of a function call to strcpy() and if
the vender supplied library uses machine specific tricks to gain speed.

But, I''ll repeat an old saying about programming.

Premature optimization is the root of all evil.
Tony Hoare, restated by Donald Knuth.

Think carefully about if this assignment to a string really matters in
terms of performance. It most likely doesn''t. Pick good algorythms for
your code and then make the implimentation of the good algorythms as clear
as possible. As a simple example, imagine the relative performance of
2 sort algorythms.

Program one.
Uses a bubble sort as its primary algorythm and is optimized as much
as possible.

Program two.
Uses a heap sort as its primary algorythm and not a lot of attention is
made to reduce CPU cycles.

For very small amounts of data, program one may be faster, but for larger
amounts of data, program two will be so much faster than program one, it
isn''t even funny. An O(N log N) algorythm vs an O(N^2) algorythm totaly
swamps any effect optimization may have.

Use the strcpy() and simply make your program readable.


这篇关于哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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