Показать сообщение отдельно
Старый 29.11.2011, 17:38   #52
HolyDel
 
Регистрация: 26.09.2006
Сообщений: 6,035
Написано 1,474 полезных сообщений
(для 2,707 пользователей)
Ответ: Великая битва 4х языков программирования на простейшей задачке

причем на плюсах:
#include <iostream>
#include <Windows.h>
#include <cstdlib>

 struct index
{
    int x, y, z;
};


int main()
{

	int count_values = 512 * 128 * 512;



    index *ordered_values = new index[count_values];
    index *random_values = new index[count_values];

    {
        int x = 0, y = 0, z = 0;

        for (int i = 0; i < count_values; ++i)
        {

            ordered_values[i].x = z;
            ordered_values[i].y = y;
            ordered_values[i].z = x;

            x++;
            if (x > 511)
            {
                x = 0;
                y++;
            }
            if (y > 127)
            {
                y = 0;
                z++;
            }


            random_values[i].x = rand()%512;
            random_values[i].y = rand()%128;
            random_values[i].z = rand()%512;
        }
    }

	int counter = 0;

	auto a = GetTickCount();

	int* map = new int[512*128*512];

	int allocate_time = GetTickCount() - a;

	a = GetTickCount();
	for(int i=0;i<20;++i)
	{
		for(int x =0;x<512;++x)
		{
			for(int y = 0;y<128;++y)
			{
				for(int z =0;z<512;++z)
				{
					++counter;
					map[512 * 128 * x + 128 * y + z] = counter;
				}
			}
		}
	}

	int fill_time = GetTickCount() - a;

	a = GetTickCount();
	int value = 0;
	for(int i=0;i<count_values;++i)
	{
		value += map[512 * 128 * ordered_values[i].x + 128 * ordered_values[i].y + ordered_values[i].z];
	}
	int ordered_time = GetTickCount() - a;
	std::cout<<"value:"<<value<<std::endl;
	a = GetTickCount();
	for(int i=0;i<count_values;++i)
	{
		value += map[512 * 128 * random_values[i].x + 128 * random_values[i].y + random_values[i].z];
	}
	int random_time = GetTickCount() - a;
	std::cout<<"value:"<<value<<std::endl;

	std::cout<<"allocate time:"<<allocate_time<<std::endl;
	std::cout<<"fill time:"<<(fill_time/20)<<std::endl;
	std::cout<<"ordered time:"<<ordered_time<<std::endl;
	std::cout<<"random time:"<<random_time<<std::endl;

	std::cin.get();
	return 0;
}
дает вполне ожидаемые 46 для ordered и 390 для random;
естественно, сюда еще входит время обращение к массиву для получения индекса.
(Offline)
 
Ответить с цитированием