मैंने हैशसेट और डिक्शनरी को बहुत सी # में उपयोग किया है, और उन्हें बहुत तेज़ पाया है ...फास्ट सी ++ कंटेनर जैसे सी # हैशसेट <T> और शब्दकोश <K,V>?
मैंने std :: map और std :: hash_map का उपयोग करने का प्रयास किया है और तुलना में उन्हें बहुत धीमा कर रहा हूं। क्या यह अपेक्षित व्यवहार की तरह लगता है? क्या std :: hash_map के उपयोग में कुछ गलत हो सकता है?
या, क्या वहां कोई बेहतर सी ++ हैश कंटेनर है?
मैं हैशिंग int32s, आमतौर पर उनमें से लगभग 100,000।
अद्यतन: मैंने सी # और सी ++ में एक रेपो बनाया। यह दो परीक्षण चलाता है, वे सी # में 1 9एमएस और 13ms लेते हैं, और सी ++ में लगभग 11,000ms।
Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms
सी ++ आउटपुट::
Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms
वहाँ कुछ वास्तव में मेरे सी ++ कोड :) (के रूप में रिलीज बनाता है दोनों का संचालन किया गया दोनों कंसोल क्षुधा हैं)
सी # आउटपुट के साथ गलत किया जाना चाहिए
सी ++ आउटपुट (stdxt :: std :: मैप के बजाय हैश_मैप का उपयोग करके)
Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms
सी ++ आउटपुट (का उपयोग कर stdext :: hash_map, एक रिलीज 64 निर्माण)
Found 292 values in the intersection, in 1037.67ms
Found 302 values in the intersection, in 3663.71ms
नोट्स:
- SET2 काफी आबादी हो रही नहीं है के रूप में मैं सी ++ में करना चाहता था, मैं यह उम्मीद कर रहा था के लिए SET1 के साथ एक 50% चौराहे (के रूप में यह सी # में करता है), लेकिन मैं किसी कारण भी पाने के लिए 10 से मेरी यादृच्छिक संख्या गुणा करने के लिए किया था उन्हें आंशिक रूप से एक दूसरे को काटना नहीं करने के लिए
सी #:
static void Main(string[] args)
{
int start = DateTime.Now.Millisecond;
int intersectionSize = runIntersectionTest();
int duration = DateTime.Now.Millisecond - start;
Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));
start = DateTime.Now.Millisecond;
intersectionSize = runIntersectionTest();
duration = DateTime.Now.Millisecond - start;
Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));
Console.ReadKey();
}
static int runIntersectionTest()
{
Random random = new Random(DateTime.Now.Millisecond);
Dictionary<int,int> theMap = new Dictionary<int,int>();
List<int> set1 = new List<int>();
List<int> set2 = new List<int>();
// Create 100,000 values for set1
for (int i = 0; i < 100000; i++)
{
int value = 1000000000 + i;
set1.Add(value);
}
// Create 1,000 values for set2
for (int i = 0; i < 1000; i++)
{
int value = 1000000000 + (random.Next() % 200000 + 1);
set2.Add(value);
}
// Now intersect the two sets by populating the map
foreach(int value in set1)
{
theMap[value] = 1;
}
int intersectionSize = 0;
foreach (int value in set2)
{
int count;
if (theMap.TryGetValue(value, out count))
{
intersectionSize++;
theMap[value] = 2;
}
}
return intersectionSize;
}
सी ++:
int runIntersectionTest()
{
std::map<int,int> theMap;
vector<int> set1;
vector<int> set2;
// Create 100,000 values for set1
for (int i = 0; i < 100000; i++)
{
int value = 1000000000 + i;
set1.push_back(value);
}
// Create 1,000 values for set2
for (int i = 0; i < 1000; i++)
{
int random = rand() % 200000 + 1;
random *= 10;
int value = 1000000000 + random;
set2.push_back(value);
}
// Now intersect the two sets by populating the map
for (vector<int>::iterator iterator = set1.begin(); iterator != set1.end(); iterator++)
{
int value = *iterator;
theMap[value] = 1;
}
int intersectionSize = 0;
for (vector<int>::iterator iterator = set2.begin(); iterator != set2.end(); iterator++)
{
int value = *iterator;
map<int,int>::iterator foundValue = theMap.find(value);
if (foundValue != theMap.end())
{
theMap[value] = 2;
intersectionSize++;
}
}
return intersectionSize;
}
int _tmain(int argc, _TCHAR* argv[])
{
srand (time(NULL));
Timer timer;
int intersectionSize = runIntersectionTest();
timer.Stop();
cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;
timer.Reset();
intersectionSize = runIntersectionTest();
timer.Stop();
cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;
getchar();
return 0;
}
क्या आप कुछ मानक प्रदान कर सकते हैं? –
सी # में शायद 10ms लगते हैं सी ++ में 1,000ms लेते हैं। मैं कल अधिक नियंत्रित तुलना करने की कोशिश करूंगा, शायद प्रत्येक सी # और सी ++ के लिए कोड पोस्ट करें। –
मैंने कुछ मानक पोस्ट किए हैं। –