/* UpTools v8.6 * * Copyright (c) 2005-2013 Fundacion Universidad de Palermo (Argentina). * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 4. Redistributions of any form whatsoever must retain the following * acknowledgment: 'This product includes software developed by the * "Universidad de Palermo, Argentina" (http://www.palermo.edu/).' * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <UpTools/UpHashMap.h> #include <UpTools/UpInet.h> #include <iostream> #include <string> using namespace std; typedef UPHASH_MAP<UpSockAddr,int,UpHash,UpEq> UpSockAddrHashMap; typedef std::map<UpSockAddr,int,UpLessThan> UpSockAddrMap; int main(int argc, char *argv[]) { if(argc!=3) { cerr<<"Usage: "<<argv[0]<<" maps-size lookup-count"<<endl; cerr<< "This program creates a __gnu_cxx::hash_map<UpSockAddr,int,UpHash,UpEq>\n" "and a std::map<UpSockAddr,int,UpLessThan> and fills them with the same\n" "maps-size generated elements. Next it makes lookup-count lookups and\n" "measures the ellapsed time and correctness"<<endl; return 1; } UpSockAddrMap sam; UpSockAddrHashMap sahm; UpSockAddrMap::iterator it; UpSockAddrHashMap::iterator iht; unsigned int b; unsigned int x6; unsigned char x4; UpSockAddr sa; union { in_addr s; unsigned char s_addr8[4]; } s4; in6_addr s6; memset((void*)&s4,0,sizeof(s4)); memset((void*)&s6,0,sizeof(s6)); unsigned short port; UpTimeVal lapse,lapse2; int mid=atoi(argv[1])/2; int tsize=mid*2; ///////////////////////////////////////////////////////////////////// vector<UpSockAddr> v(tsize); cout<<"generating "<<(tsize)<<" UpSockAddr's"<<endl; lapse.tic(); for(int i=0; i<mid; i++ ) { b=(unsigned int)i%4; port=i%0xffff+1; x4=(unsigned char)i*0xf; s4.s_addr8[b]=x4; sa.set(s4.s,port); v[i]=sa; // cout<<sa<<endl; x6=(unsigned int)i*0xffffff; s6.s6_addr32[b]=x6; sa.set(s6,port); v[mid+i]=sa; // cout<<sa<<endl; } lapse.toc(); cout<<"Elapsed: "<<lapse<<endl<<endl; ///////////////////////////////////////////////////////////////////// cout<<"filling std::map<UpSockAddr,int,UpLessThan>" " with "<<tsize<<" elements"<<endl; lapse.tic(); for(vector<UpSockAddr>::iterator iv=v.begin(); iv!=v.end(); iv++ ) { sam[*iv]=iv->port(); } lapse.toc(); cout<<"Elapsed: "<<lapse<<endl; ///////////////////////////////////////////////////////////////////// cout<<"filling __gnu_cxx::hash_map<UpSockAddr,int,UpHash,UpEq>" " with "<<mid*2<<" elements"<<endl; lapse2.tic(); for(vector<UpSockAddr>::iterator iv=v.begin(); iv!=v.end(); iv++ ) { sahm[*iv]=iv->port(); } lapse2.toc(); cout<<"Elapsed: "<<lapse2<<endl; cout<<"Ratio: "<<(lapse/lapse2)<<endl<<endl; ///////////////////////////////////////////////////////////////////// int lups=atoi(argv[2]); int lookedup; cout<<"Making "<<lups<<" lookups in std::map<UpSockAddr,int,UpLessThan>"<<endl; iht=sahm.begin(); lapse.tic(); for(int i=0; i<lups; i++ ) { if(iht==sahm.end()) iht=sahm.begin(); lookedup=sam[iht->first]; // here is the lookup if( lookedup != iht->second ) { cerr<<"error: \nsahm["<<iht->first<<"] = "<<iht->second <<"\nsam["<<iht->first<<"] = "<<lookedup<<endl; return 2; } iht++; } lapse.toc(); cout<<"Elapsed: "<<lapse<<endl; ///////////////////////////////////////////////////////////////////// cout<<"Making "<<lups<<" lookups in __gnu_cxx::hash_map<UpSockAddr,int,UpHash,UpEq>"<<endl; it=sam.begin(); lapse2.tic(); for(int i=0; i<lups; i++ ) { if(it==sam.end()) it=sam.begin(); lookedup=sahm[it->first]; // here is the lookup if(lookedup != it->second ) { cerr<<"error: \nsam["<<it->first<<"] = "<<it->second <<"\nsahm["<<it->first<<"] = "<<lookedup<<endl; return 2; } it++; } lapse2.toc(); cout<<"Elapsed: "<<lapse2<<endl; cout<<"Ratio: "<<(lapse/lapse2)<<endl<<endl; ///////////////////////////////////////////////////////////////////// return 0; }