Exemple #1
0
void BenchRandomDigit(void(*f)(double, char*), const char* fname, FILE* fp) {
	printf("Benchmarking randomdigit %-20s ... ", fname);

	char buffer[256];
	double minDuration = std::numeric_limits<double>::max();
	double maxDuration = 0.0;

	for (int digit = 1; digit <= RandomDigitData::kMaxDigit; digit++) {
		double* data = RandomDigitData::GetData(digit);
		size_t n = RandomDigitData::kCount;

		double duration = std::numeric_limits<double>::max();
		for (unsigned trial = 0; trial < kTrial; trial++) {
			Timer timer;
			timer.Start();

			for (unsigned iteration = 0; iteration < kIterationPerDigit; iteration++) {
				for (size_t i = 0; i < n; i++) {
					f(data[i], buffer);
					//if (trial == 0 && iteration == 0 && i == 0)
					//	printf("%.17g -> %s\n", data[i], buffer);
				}
			}

			timer.Stop();
			duration = std::min(duration, timer.GetElapsedMilliseconds());
		}

		duration *= 1e6 / (kIterationPerDigit * n); // convert to nano second per operation
		minDuration = std::min(minDuration, duration);
		maxDuration = std::max(maxDuration, duration);
		fprintf(fp, "randomdigit,%s,%d,%f\n", fname, digit, duration);
	}
	printf("[%8.3fns, %8.3fns]\n", minDuration, maxDuration);
}
Exemple #2
0
void BenchRandom(void(*f)(double, char*), const char* fname, FILE* fp) {
	printf("Benchmarking      random %-20s ... ", fname);

	char buffer[256];
	double* data = RandomData::GetData();
	size_t n = RandomData::kCount;

	double duration = std::numeric_limits<double>::max();
	for (unsigned trial = 0; trial < kTrial; trial++) {
		Timer timer;
		timer.Start();

		for (unsigned iteration = 0; iteration < kIterationForRandom; iteration++)
			for (size_t i = 0; i < n; i++)
				f(data[i], buffer);

		timer.Stop();
		duration = std::min(duration, timer.GetElapsedMilliseconds());
	}

	duration *= 1e6 / (kIterationForRandom * n); // convert to nano second per operation

	fprintf(fp, "random,%s,0,%f\n", fname, duration);

	printf("%8.3fns\n", duration);
}
Exemple #3
0
void BenchSequential(void(*f)(double, char*), const char* fname, FILE* fp) {
	printf("Benchmarking  sequential %-20s ... ", fname);

	char buffer[256] = { '\0' };
	double minDuration = std::numeric_limits<double>::max();
	double maxDuration = 0.0;

	int64_t start = 1;
	for (int digit = 1; digit <= 17; digit++) {
		int64_t end = start * 10;

		double duration = std::numeric_limits<double>::max();
		for (unsigned trial = 0; trial < kTrial; trial++) {
			int64_t v = start;
			Random r;
			v += ((int64_t(r()) << 32) | int64_t(r())) % start;
			double sign = 1;
			Timer timer;
			timer.Start();
			for (unsigned iteration = 0; iteration < kIterationPerDigit; iteration++) {
				double d = v * sign;
				f(d, buffer);
				//printf("%.17g -> %s\n", d, buffer);
				sign = -sign;
				v += 1;
				if (v >= end)
					v = start;
			}
			timer.Stop();
			duration = std::min(duration, timer.GetElapsedMilliseconds());
		}

		duration *= 1e6 / kIterationPerDigit; // convert to nano second per operation
		minDuration = std::min(minDuration, duration);
		maxDuration = std::max(maxDuration, duration);
		fprintf(fp, "%s_sequential,%d,%f\n", fname, digit, duration);
		start = end;
	}

	printf("[%8.3fns, %8.3fns]\n", minDuration, maxDuration);
}
Exemple #4
0
bool CGUL::Network::HTTPRequest::PerformRequest(UInt32 timeout)
{
    response = "";
    responseHead = "";
    responseBody = "";

    if (!sock->IsConnected())
    {
        throw NetworkException(NetworkExceptionCode::FAILED_HTTP_REQUEST, NetworkExceptionReason::SOCKET_INVALID);
    }

    sock->Send((const void*)request.GetCString(), request.GetSize());

    char buffer[1024];

    // Wait for the response.
    Timer timeoutTimer;
    timeoutTimer.Start();
    while (sock->Peek(buffer, 1) == 0 && (timeoutTimer.GetElapsedMilliseconds() < timeout || timeout == 0))
    {
        if (!sock->IsConnected())
        {
            throw NetworkException(NetworkExceptionCode::FAILED_HTTP_REQUEST, NetworkExceptionReason::SOCKET_INVALID);
        }
    }
    timeoutTimer.Stop();

    if (timeoutTimer.GetElapsedMilliseconds() >= timeout && timeout != 0)
    {
        throw NetworkException(NetworkExceptionCode::FAILED_HTTP_REQUEST, NetworkExceptionReason::TIMEOUT);
    }

    //Get the headers length.
    int headSize = 0;
    sock->Peek(buffer, 1024);
    CGUL::String bufferString = buffer;
    headSize = bufferString.FindFirstOf("\r\n\r\n")+4;

    //Get the reponse's head.
    char* bufferHead = new char[headSize];
    sock->Receive(bufferHead, headSize);
    responseHead += bufferHead;

    //Parse the Response Header
    ParseResponseHead();

    //Get the reponse's body.
    int amount = 0;

    switch (header.transferEncoding)
    {
        case HTTPTransferEncoding::CONTENT_LENGTH:
        {
            unsigned int count = 0;
            while (sock->IsConnected())
            {
                unsigned int size = 1024;
                if (count + size > header.contentLength)
                {
                    size = header.contentLength - count;
                }

                char* buff = new char[size];
                amount = sock->Receive((void*)buff, size);
                count += amount;
                if (amount > 0)
                {
                    responseBody += EncodeString(buff, size);
                }

                if (count >= header.contentLength)
                {
                    break;
                }
            }
            break;
        }

        case HTTPTransferEncoding::CHUNKED:
        {
            char * sizeBuffer = new char[64];

            while (sock->IsConnected())
            {
                //Get the size.
                amount = sock->Peek(sizeBuffer, 64);
                if (amount <= 0)
                {
                    break;
                }

                String sizeString = "";
                for (int i = 0; i < amount; i++)
                {
                    if (sizeBuffer[i] == '\r')
                    {
                        if (sizeBuffer[i+1] == '\n')
                        {
                            break;
                        }
                        throw NetworkException(NetworkExceptionCode::FAILED_HTTP_REQUEST, NetworkExceptionReason::UNKNOWN);
                    }
                    else
                    {
                        sizeString += sizeBuffer[i];
                    }
                }
                //Convert from hex to decimal
                unsigned int size = 0;
                for (unsigned j = 0; j < sizeString.GetLength(); j++)
                {
                    char code = sizeString[sizeString.GetLength()-j-1];
                    if (code >= 48 && code <= 57) //Number
                    {
                        size += (code - 48)*((j == 0) ? 1 : 16*j);
                    }
                    if (code >= 65 && code <= 70) //A-F
                    {
                        size += (code - 55)*((j == 0) ? 1 : 16*j);
                    }
                    if (code >= 97 && code <= 102) //a-f
                    {
                        size += (code - 87)*((j == 0) ? 1 : 16*j);
                    }
                }
                sock->Receive(sizeBuffer, sizeString.GetLength()+2);

                if (size == 0) //If the size is 0, then we are done
                {
                    break;
                }
                size += 2;

                //Otherwise receive the next chunk and add a new line (\r\n)
                char* buff = new char[size];
                amount = sock->Receive((void*)buff, size);
                responseBody += EncodeString(buff, size);
            }
            break;
        }

        default:
        {
            throw NetworkException(NetworkExceptionCode::FAILED_HTTP_REQUEST, NetworkExceptionReason::UNKNOWN_TRANSFER_ENCODING);
            break;
        }
    }

    response = responseHead + responseBody;

    if (header.connection == Network::HTTPConnections::CLOSE)
    {
        Close();
    }

    return true;
}