需要一种优化的方法 [英] need an optimized method

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

问题描述




我需要做以下操作:


''"我有两个mac地址,比如说X和Y,其中X是基本的mac

地址,Y是来自X的第n个mac地址,每个地址都加1。

现在,我想检查Z是否在[X,Y]范围内。只有当Z是此范围内的mac地址之一时,我才需要检查

。我正在寻找

的优化方法来执行此操作

检查将在高优先级回调任务中调用,并且

简单地比较mac地址将导致这个任务的一些性能问题。


有人可以指导/帮助我吗?


Sekia

解决方案

文章< 11 ********** ************@z28g2000prd.googlegroups .com> ;,

< la *************** @ gmail.comwrote :


>

我需要做以下操作:

''"我有两个mac地址,比如X和Y,其中X是基本的mac
地址,Y是来自X的第n个mac地址,每个都递增1。
现在,我想检查是否Z落在[X,Y]之内。只有当Z是此范围内的mac地址之一时,我才需要检查
。我正在寻找一个优化方法来执行此操作,因为这将在高优先级回调任务中调用,并且简单地比较mac地址将导致
这项任务的一些性能问题。

有人可以指导/帮助我解决这个问题吗?



只有当他们在你关心的范围内时才进行比较。


(或者更严重的是:图你正在尝试做什么和

真正的限制是什么。然后测量它是否需要

聪明。然后检查是否有一个更好的方法来实现

目标,而不是微观优化你所拥有的解决方案。那么,如果你确定

你真的需要微优化,找一个人知道他们做了什么

这样做。)

dave


-

Dave Vandervies dj******@csclub.uwaterloo.ca


C99。 C99跑。运行,99,运行。

- 在comp.lang.c中的Joona I Palaste


la *************** @ gmail.com 写道:
< blockquote class =post_quotes>



我需要做以下操作:


''"我有两个mac地址,比如说X和Y,其中X是基本的mac

地址,Y是来自X的第n个mac地址,每个地址都加1。

现在,我想检查Z是否在[X,Y]范围内。只有当Z是此范围内的mac地址之一时,我才需要检查

。我正在寻找

的优化方法来执行此操作

检查将在高优先级回调任务中调用,并且

简单地比较mac地址将导致这个任务的一些性能问题。


有人可以指导/帮助我吗?


Sekia



if(Z> = X&& Z< = Y){

}


这只需要两次整数比较。现在几乎没什么大不了的。

我无法想象这两个整数比较会使你的程序实际速度产生任何差异。


为什么你认为是这种情况?


你做了什么测量来证明这个论点?


jacob


文章< 46 *********************** @ news.orange.fr> ;,

jacob navia< ja *** @ jacob.remcomp.frwrote:


> la ****** *********@gmail.com写道:


>''"我有两个mac地址,比如X和Y,其中X是基本的mac
地址,Y是来自X的第n个mac地址,每个都递增1。
现在,我想检查是否Z落在[X,Y]之内。只有当Z是此范围内的mac地址之一时,我才需要检查


> if(Z> = X&& Z< = Y){
}


>这只需要两次整数比较。现在几乎没什么大不了的。
我无法想象这两个整数比较会使程序的实际速度产生任何差异。



MAC地址是多字节非算术类型。 OP的特定应用程序可能需要修复

字节的数量,

但*在一般情况下*媒体访问控制(MAC)大小可能会有所不同<媒体之间的
。包含二进制0'的字节在MAC地址中并不罕见

,所以我们不能只是简单地进行字符串操作。


如果速度发生比存储更重要,并且

相同的[X,Y]对经常被检查,然后我会向OP建议操作可以加速通过保持

每个[X,Y]对的一个索引,该对X和Y的共同字节数为
。从头开始比较字节,如果在前N个字节中存在不匹配的
,则MAC不在该范围内;从

N到最后,你需要一个范围检查(两个比较而不是一个。)


但是如果速度非常重要,那么它就变得很重要了询问

各种情况的可能性;如果很少有传入的

MAC将在该范围内,那么您的最佳代码将与大多数MAC将在该范围内的情况不同。当然,

可用空间也是一个因素 - 通过在部分MAC上使用查找表,可以使某些事情变得更快

,但是有空间
这种权衡?并且只有一个[X,Y]对或

可以有很多这样的对吗?有很多这样的对的解决方案可能会有很大不同,可能是MAC中字节数的时间线性而不是时​​间线性的/>
要检查的这类对的数量;然后有

算法,这些算法是要检查的对数的字节数* log(n)

但是空间紧凑......

-

重要的是要记住,在法律方面,电脑

永远不会复制,只有人类才能制作复制品。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton


Hi,

I need to do the following operation :

''" I have two mac addresses, say X and Y,where X is the base mac
address, and Y is the nth mac address from X, each incremented by one.
Now,I want to check if Z falls within [X,Y]. I need to do some check
only when Z is one of the mac addresses in this range. I am looking
for an optimized method to do this as this
check is going to be called in a high priority callback task, and
simply comparing the mac address will lead to
some performance issues to this task ".

Can someone guide/help me out on this ?

Sekia

解决方案

In article <11**********************@z28g2000prd.googlegroups .com>,
<la***************@gmail.comwrote:

>Hi,

I need to do the following operation :

''" I have two mac addresses, say X and Y,where X is the base mac
address, and Y is the nth mac address from X, each incremented by one.
Now,I want to check if Z falls within [X,Y]. I need to do some check
only when Z is one of the mac addresses in this range. I am looking
for an optimized method to do this as this
check is going to be called in a high priority callback task, and
simply comparing the mac address will lead to
some performance issues to this task ".

Can someone guide/help me out on this ?

Only compare them if they''re in the range you care about.

(Or, more seriously: Figure out what you''re Really Trying To Do and
what the constraints Really Are. Then measure it to see if you need to
be clever. Then check to see if there''s a better way to accomplish the
goal than microoptimizing the solution you have. THEN, if you''re sure
you really need to microoptimize, find somebody who knows what they''re
doing to do it.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

C99. C99 run. Run, 99, run.
--Joona I Palaste in comp.lang.c


la***************@gmail.com wrote:

Hi,

I need to do the following operation :

''" I have two mac addresses, say X and Y,where X is the base mac
address, and Y is the nth mac address from X, each incremented by one.
Now,I want to check if Z falls within [X,Y]. I need to do some check
only when Z is one of the mac addresses in this range. I am looking
for an optimized method to do this as this
check is going to be called in a high priority callback task, and
simply comparing the mac address will lead to
some performance issues to this task ".

Can someone guide/help me out on this ?

Sekia

if (Z >= X && Z <= Y) {
}

This needs only two integer comparisons. Hardly a big deal now.
I can''t imagine that those two integer comparisons make ANY
difference in the actual speed of your program.

Why do you think that that is the case?

What measurements have you done to prove that thesis?

jacob


In article <46***********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:

>la***************@gmail.com wrote:

>''" I have two mac addresses, say X and Y,where X is the base mac
address, and Y is the nth mac address from X, each incremented by one.
Now,I want to check if Z falls within [X,Y]. I need to do some check
only when Z is one of the mac addresses in this range.

>if (Z >= X && Z <= Y) {
}

>This needs only two integer comparisons. Hardly a big deal now.
I can''t imagine that those two integer comparisons make ANY
difference in the actual speed of your program.

MAC addresses are multibyte non-arithmetic types. The number of
bytes involved might be fixed for the OP''s particular application,
but *in the general case* Media Access Control (MAC) sizes could vary
between media. Bytes containing binary 0''s are not uncommon
in MAC addresses, so we can''t just drop down to string operations.

If speed happens to be more important than storage, and the
same [X,Y] pair gets checked against often, then I would
suggest to the OP that operations could be speed up by keeping
an index per [X,Y] pair of the number of bytes in common that X and Y
have. Compare bytes from the beginning, and if there is a mismatch
in the first N bytes then the MAC is not within the range; from
N to the end, you need a range check (two comparisons instead of one.)

But if speed is very important, then it becomes important to ask what
the probabilities are of various cases; if few of the incoming
MACs will be in the range, then your optimal code would be different
than if most of the MACs will be in the range. And of course
available space is a factor too -- some things can be made faster
by using lookup tables on portions of the MACs, but is there room
for that kind of tradeoff? And is there only one [X,Y] pair or
could there be a lot of such pairs? The solution when there are
a lot of such pairs might be quite different, possibly being
time-linear in the number of bytes in the MACs rather than time-linear
in the number of such pairs to be examined; then there are the
algorithms that are bytes*log(n) in the number of pairs to be examined
but are fairly space compact...
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


这篇关于需要一种优化的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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