Is this possible to optimize this method ?

S

Steeve

Hi,

Is this possible to optimize this method ? It's the method than I call
most offen in my application (result of Sampling Profiling in Visual
Studio Performance report)

I use this method to find a particular Node in a List<Node>.
(Note: JobNumber is a int)

public bool NodeMachineJobPredicate(Node N)
{
if (string.Equals(_Machine,N.Machine) &&
_Job.JobNumber == N.Job.JobNumber)
{
return true;
}
else
{
return false;
}
}

Thanks
Steeve
 
B

Barry Kelly

Steeve said:
Is this possible to optimize this method ? It's the method than I call
most offen in my application (result of Sampling Profiling in Visual
Studio Performance report)

I use this method to find a particular Node in a List<Node>.

You could consider using a Dictionary said:
(Note: JobNumber is a int)

public bool NodeMachineJobPredicate(Node N)
{
if (string.Equals(_Machine,N.Machine) &&
_Job.JobNumber == N.Job.JobNumber)
{
return true;
}
else
{
return false;
}
}

I would write this as:

return _Job.JobNumber == N.Job.JobNumber
&& string.Equals(_Machine, N.Machine, StringComparison.Ordinal);

.... if I knew that an ordinal string comparison was ok. However, I'd
look to make sure you're either using a binary search or a hash table
(e.g. Dictionary<,>) first, to take care of the algorithmic complexity.

You can make a special-purpose key for the dictionary out of the pair of
int & string without too much difficulty.

-- Barry
 
M

Markus Grueneis

Steeve said:
Hi,

Is this possible to optimize this method ? It's the method than I call
most offen in my application (result of Sampling Profiling in Visual
Studio Performance report)

I use this method to find a particular Node in a List<Node>.
(Note: JobNumber is a int)

First, maybe you don't want a list. Maybe you can hash
(Machine . JobNumber) as key, or can have an additional hashtable which
indexes this pair, and points to the "real" elements in a list.
public bool NodeMachineJobPredicate(Node N)
{
if (string.Equals(_Machine,N.Machine) &&
_Job.JobNumber == N.Job.JobNumber)
{

Second: if you really want a list, check for the number first, and the
string afterwards, as number comparisons should be cheaper.


Best regards,
-- Markus
 
M

Markus Stoeger

Steeve said:
Hi,

Is this possible to optimize this method ? It's the method than I call
most offen in my application (result of Sampling Profiling in Visual
Studio Performance report)

I use this method to find a particular Node in a List<Node>.

You could use a Dictionary with N.Machine and N.Job.JobNumber as keys
and the Nodes as values. That should be the fastest for searching for Nodes.

If your list is sorted you could also use the BinarySearch method to
quickly find Nodes. Thats a bit slower than the Dictionary, but still
faster than the linear search (with a simple for/foreach loop).

hth,
Max
 
C

Cor Ligthert [MVP]

Steeve,

How many picoseconds you expect to optimize this using this 100000 times?

Although the simplest optimization is of course to set the fastest variables
to test first, the Or is shortcutted which means that the second is not done
if false.

Just my thought reading our message.

Cor
 
C

Christof Nordiek

Hi Steve,

in addition to all what is said until now, i would stringly recommand to
performance test the solutions with typical data. The effect on performance
strongly depends on the data and almost any solution could be less
performant
as any other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top