Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I am wondering if there is an efficient algorithm to compare two vectors. The purpose of what I am trying to accomplish is to check if two vectors are the same. This code is attended to override the == operator.

If anyone has any suggestions or advice I would much appreciated it because the method I am trying now is far to slow to compare 10000 elements.

Here is the code I have now:

C#
inline bool operator == (Journal const& lhs, Journal const& rhs)
{
        if (lhs.get_record_size() != rhs.get_record_size())
                return false;
        record_size num = lhs.get_record_size();
        for(record_size i = 0; i < num; ++i)
        {
                if (lhs.get_record()[i]!= rhs.get_record()[i])
                        return false;
        }
        return true;
}



Everyone's time is greatly appreciated,
Thanks!

robNO.

UPDATED CODE ( THAT IS MUCH FASTER ):

C#
inline bool operator == (Journal const& lhs, Journal const& rhs)
{
        if (lhs.get_record_size() != rhs.get_record_size())
                return false;
        if (lhs.get_record() != rhs.get_record())
                return false;
        return true;
}


More advice is appreciated!

Thanks Everyone!!!
robNO.
Posted
Updated 23-Jan-11 16:00pm
v4
Comments
Maximilien 23-Jan-11 20:35pm    
What is get_record() ? does it return an object ? a reference to an object ? a pointer to an object ?

Are the two vectors sorted ? are the vector "equal" if members are the same but inserted in different position ?

M.
RobNO 23-Jan-11 20:50pm    
Thanks for your reply! I am sorry that I was not very specific and I have made some changes to the code which is posted.

In the updated code block, get_record returns a reference to vector of Records. A Record is a string. The vectors are not required to be sorted (yet anyways). The vector is not equal if the members are the same but inserted in a difference position ( This would be great, however).

Thanks,
robNO.

Why not just use lhs==rhs?
 
Vectors have an operator==.
 
 
Share this answer
 
Comments
RobNO 23-Jan-11 21:58pm    
Thanks for your reply!

That fixes my problem because it is A LOT quicker!!! I feel like an idiot for not even trying that.

Thank your help is greatly appreciated!!!
robNO.
fjdiewornncalwe 23-Jan-11 22:27pm    
@RobNO: How about marking this as "answered" for Stephen. Cheers.
Sergey Alexandrovich Kryukov 23-Jan-11 23:30pm    
One needs to be able to see - a 5.
--SA
I would time using iterators instead.

If not using iterators, then I might at least use


1) size_t const record_size=lhs.get_record_size();

before the loop. That way it is clearer to the optimizer that the value does not change in the loop.

2) Depending on how simple your class is, you might be able to use memcmp in your comparison operator.
If vector actually allocated all of the memory in a contiguous physical block then you could have an easy check using memcmp.

Note the following (Herb Sutter)
VB
It’s so important, in fact, that when it was discovered that the C++98 standard didn’t completely guarantee contiguity, the C++03 standard was amended to explicitly add the guarantee.
 
Share this answer
 
v2
Comments
RobNO 23-Jan-11 20:03pm    
Thanks for your reply.

I am having trouble figuring out how to use memcmp ( I have never used it before). So maybe if I give you a little more detail about my classes you could assist me? So basically I have a class called Journal that has a vector of Records. The Records class is just holds a string. To access the vector of records I do this: lhs.get_record(). To access the Records String I do this: lhs.get_record()[1].get_record().

I hope this is enough information if not tell me what you would need to know in order to use memcmp.

Thanks for your time!
robNO.
T2102 23-Jan-11 20:12pm    
http://www.cplusplus.com/reference/clibrary/cstring/memcmp/
However, you have to be sure that you can use memcmp for your class. For many classes, this is not the case. If it's members could be represented by struct without pointers and without members that use pointers, then it may be possible. You should compare the results for a few test cases to make sure.
RobNO 23-Jan-11 20:24pm    
I am not sure how to use that method with vectors. I will be using your first advice, however.

Thanks for your time.
robNO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900