c++中map的find与count性能对比

{
	using namespace std::chrono;

	std::map nummap1;
	{
		std::cout << "build one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			nummap1[i] = i + 1;
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl;
	}

	std::map nummap2;
	{
		std::cout << "build two map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			nummap2[i] = i + 1;
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl << std::endl;
	}



	{
		std::cout << "find  one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			nummap1.find(i);
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl;
	}

	{
		std::cout << "count one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			nummap2.count(i);
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl << std::endl;
	}



	{
		std::cout << "find  one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			auto it = nummap1.find(i);
			if (it != nummap1.end());
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl;
	}

	{
		std::cout << "count one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			auto it = nummap2.count(i);
			if (it != 0);
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl << std::endl;
	}



	{
		std::cout << "find  one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			auto it = nummap1.find(i);
			if (it != nummap1.end())
			{
				auto val = it->second;
			}
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl;
	}

	{
		std::cout << "count one map  time:";
		auto start = high_resolution_clock::now();
		for (size_t i = 0; i < 100000; i++)
		{
			auto it = nummap2.count(i);
			if (it != 0)
			{
				auto val = nummap2.at(i);
			}
		}
		auto site1 = high_resolution_clock::now();
		std::cout << duration_cast(site1 - start).count() << "  nanoseconds passed!" << std::endl;
	}

}

运行结果:

build one map  time:1933090922  nanoseconds passed!
build two map  time:1913304601  nanoseconds passed!

find  one map  time:726226943  nanoseconds passed!
count one map  time:1254866463  nanoseconds passed!

find  one map  time:820331007  nanoseconds passed!
count one map  time:1255967332  nanoseconds passed!

find  one map  time:794207291  nanoseconds passed!
count one map  time:1983391057  nanoseconds passed!

你可能感兴趣的:(C++)