473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

need an optimized method

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

Jul 10 '07 #1
11 1569
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
Jul 10 '07 #2
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
Jul 10 '07 #3
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
Jul 10 '07 #4
jacob navia <ja***@jacob.remcomp.frwrites:
la***************@gmail.com wrote:
>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 ".
[...]
>
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.
You're assuming that a "mac address" is represented as an integer, or
at least as some arithmetic type.

The term usually refers to the 48-bit unique address of a network
adapter; this can usually be represented as an integer (assuming
either a C99 implementation or one that has an integer type of at
least 48 bits).

But from the way the original poster asked the question, he may be
talking about machine addresses, i.e., raw memory addresses, though
the phrase "each incremented by one" seems out of place. If so, he
needs to be aware that relational operators on pointers invoke
undefined behavior *unless* both pointers point into the same object,
or just past the end of it. (Such comparisons are likely to work as
expected in most implementations; if the OP is willing to accept code
that isn't 100% portable, the simple comparisons you suggest might do
the job.)

The OP needs to clarify just what he's talking about.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 10 '07 #5
On Jul 10, 11:57 am, lakshmiram.sai...@gmail.com wrote:
>
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 ?
Has the size of the problem been measured? What is the current
implementation, and by how much do you need to speed it up?

It depends very much on how you represent the mac addresses in this
code. If it really is the case that simply comparing the addresses
will cause performance problems, then you very likely need micro-
optimizations beyond the scope of this newsgroup - they'll depend on
the instruction set in question, the cache model of the processor in
question, locations of variables in memory, the compiler you are
using, and so on.

More realistically (but perhaps less practically) if this really is a
problem you need to implement the software on a more powerful platform.

Jul 11 '07 #6
On Jul 11, 4:19 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <46940067$0$25915$ba4ac...@news.orange.fr>,
jacob navia <j...@jacob.remcomp.frwrote:
lakshmiram.sai...@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.
Thanks,and Yes, your understanding is exact.Mac address is 48 bytes of
size,
and zero mac can also be there. To give you more information, assuming
that
the I have a base mac = 00:00:00:00:00:01.I have some 24 physical
interfaces,
each having a mac such that , nth mac = base mac + n , n<=24.

This is basically real-time scenario where, the callback task will be
hit
lots of time for each incmng pkt. I need to do the following :

1. Get the incoming pkts src mac (assume that I got it)
2. If this src mac falls within base mac or last physical interface
mac, then
execute some actions.

The range checking of the src mac is what I want to optimise.
Also,since the base mac
is user configurable, the comparsion of first nth bytes will not help,
since, assume,
base mac configured by user is = 00:00:00:00:FF:FF, then next mac will
be 00:00:00:01:00:00
Now, if the cmpare the first 4 bytes, then this will fail, and i will
not be doing the chck I want to perform.!

Pls let me know what I can do more in this case.
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

Jul 11 '07 #7
la***************@gmail.com writes:
On Jul 11, 4:19 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <46940067$0$25915$ba4ac...@news.orange.fr>,
jacob navia <j...@jacob.remcomp.frwrote:
>lakshmiram.sai...@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.
Thanks,and Yes, your understanding is exact.Mac address is 48 bytes
of size, and zero mac can also be there. To give you more
information, assuming that the I have a base mac =
00:00:00:00:00:01.I have some 24 physical interfaces, each having a
mac such that , nth mac = base mac + n , n<=24.
[...]

A mac address is 48 bits, not 48 bytes.

So you're dealing with 48-bit numbers. Your C compiler probably
supports an integer type at least that big. Treat the mac addresses
as integers, and compare them. I doubt that there's much you can do
to improve on that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 11 '07 #8
lakshmiram.sai...@gmail.com wrote:

<snip>
>lakshmiram.sai...@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)
<snip>
To give you more information, assuming that the I have a base
mac = 00:00:00:00:00:01.I have some 24 physical interfaces,
each having a mac such that , nth mac = base mac + n , n<=24.

This is basically real-time scenario where, the callback task will be hit
lots of time for each incmng pkt. I need to do the following :

1. Get the incoming pkts src mac (assume that I got it)
2. If this src mac falls within base mac or last physical interface
mac, then execute some actions.

The range checking of the src mac is what I want to optimise.
Also,since the base mac is user configurable, the comparsion
of first nth bytes will not help, since, assume, base mac
configured by user is = 00:00:00:00:FF:FF, then next mac will
be 00:00:00:01:00:00

Now, if the cmpare the first 4 bytes, then this will fail, and i will
not be doing the chck I want to perform.!

Pls let me know what I can do more in this case.
Other than what the others have said, if, after profiling, you
determine that micro-optimisation of this routine is necessary,
considering compiler intrinsics or inline assembly is a possibility.
If the platform is Intel based then I recommend comp.lang.asm.x86. You
might be able to make more efficient comparisons by using platform
specific instructions and registers.

Jul 11 '07 #9
Walter Roberson wrote:
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.
They would never go beyond 64 bits, and if the compiler supports
long long (simulated) those integer comparisons will be faster
than many other algorithms if the machine is a 32 bit computer.

In a 32 bit computer we have 4 integer comparisons in the
worst case, and mostly just 2-3 will be enough.
Jul 11 '07 #10
On Wed, 11 Jul 2007 01:27:37 -0700, Keith Thompson wrote:
la***************@gmail.com writes:
>On Jul 11, 4:19 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>>In article <46940067$0$25915$ba4ac...@news.orange.fr>,
jacob navia <j...@jacob.remcomp.frwrote:

lakshmiram.sai...@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) {
}
<snip>
>Thanks,and Yes, your understanding is exact.Mac address is 48 bytes
of size, and zero mac can also be there. To give you more
information, assuming that the I have a base mac =
00:00:00:00:00:01.I have some 24 physical interfaces, each having a
mac such that , nth mac = base mac + n , n<=24.

[...]

A mac address is 48 bits, not 48 bytes.

So you're dealing with 48-bit numbers. Your C compiler probably
supports an integer type at least that big. Treat the mac addresses
as integers, and compare them. I doubt that there's much you can do
to improve on that.
Supposing that long is big enough, that long b is your base mac address
and that long t is your last mac address you could compute
unsigned long d = t-b before hand and then when a candidate mac address x
comes along test it with (unsigned long)(x-b) <= d (the point being that
if x>b then x-b < 0, so as an unsigned is bigger than d).
But is such a scurvy trick standard C? Will it be faster than two
compares?
Duncan
Jul 11 '07 #11
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.
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 ?
What's wrong with:

if ((Z >= X) && (Z <= Y)) whatever();

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jul 11 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
16
by: Joel Finkel | last post by:
Folks, I am confused as to how to implement the following solution. I have a series of processing steps, each of which contains similar features (forms, etc). Therefore, I create a base...
9
by: cj | last post by:
I'm trying to forge ahead with Visual Basic .Net but recently I've suffered several major set backs in demonstrating VB is the future and we should move from Visual FoxPro. I really need to find...
10
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005...
2
by: steevehetu18 | last post by:
Hi, I'm doing a algorithm to calcule Earliest Start et Latest Start for a Graph with Nodes and Arcs. (like a PERT diagram) . Unfortunatly, i receive a wierd exception message for a specific...
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
1
by: jpw | last post by:
I am writing a Python / C++ embed app and it need to work on 3 platforms I have the PYTHONPATH variable set correctly and have gone back and downloaded compiled and installed the latest Python...
7
by: Zytan | last post by:
I have the simplified build ("show advanced build configurations" turned off), so that pressing F5 runs in DEBUG mode with the debugger. When an assertion fires, I find that I cannot 'watch' some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.