Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created December 11, 2019 16:30
Show Gist options
  • Save untodesu/e2d428a690cb972e4b21a9669fc02f88 to your computer and use it in GitHub Desktop.
Save untodesu/e2d428a690cb972e4b21a9669fc02f88 to your computer and use it in GitHub Desktop.
//================================
// The AUX2D Game Stub
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/
//
// File: Temporary entry point
//================================
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <chrono>
#include <amalloc.h>
static constexpr long long COUNT = 2000;
static constexpr size_t BYTES_MIN = 8;
static constexpr size_t BYTES_MAX = 1024 * 10;
int main(int argc, char **argv)
{
std::srand((unsigned int)std::time(0));
//Average for C++ new
uint8_t *cpp_arr = nullptr;
float cpp_avg = 0.0f;
for(long long i = 0; i < COUNT; i++) {
auto start = std::chrono::high_resolution_clock::now();
cpp_arr = new uint8_t[std::rand() % BYTES_MAX + BYTES_MIN];
auto end = std::chrono::high_resolution_clock::now();
delete[] cpp_arr;
cpp_avg += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
cpp_avg /= COUNT;
std::printf("CPP AVG: %f\n", cpp_avg);
//Average for C malloc
uint8_t *c_arr = nullptr;
float c_avg = 0.0f;
for(long long i = 0; i < COUNT; i++) {
auto start = std::chrono::high_resolution_clock::now();
c_arr = (uint8_t *)malloc(std::rand() % BYTES_MAX + BYTES_MIN);
auto end = std::chrono::high_resolution_clock::now();
free(c_arr);
c_avg += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
c_avg /= COUNT;
std::printf("C AVG: %f\n", c_avg);
H_Initialize(BYTES_MAX);
//Average for H_Malloc
uint8_t *h1_arr = nullptr;
float h1_avg = 0.0f;
for(long long i = 0; i < COUNT; i++) {
auto start = std::chrono::high_resolution_clock::now();
h1_arr = (uint8_t *)H_Malloc(std::rand() % BYTES_MAX + BYTES_MIN);
auto end = std::chrono::high_resolution_clock::now();
H_Free();
h1_avg += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
h1_avg /= COUNT;
std::printf("H_Malloc AVG: %f\n", h1_avg);
//Average for H_HighMalloc
uint8_t *h2_arr = nullptr;
float h2_avg = 0.0f;
for(long long i = 0; i < COUNT; i++) {
auto start = std::chrono::high_resolution_clock::now();
h2_arr = (uint8_t *)H_HighMalloc(std::rand() % BYTES_MAX + BYTES_MIN);
auto end = std::chrono::high_resolution_clock::now();
H_HighFree();
h2_avg += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
h2_avg /= COUNT;
std::printf("H_HighMalloc AVG: %f\n", h2_avg);
H_Shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment