#include <cstdio> // for printf.
#include <chrono> // for milliseconds, steady_clcok, duration_cast.
const int N = 10 * 1000;
struct point
{
point(double x, double y)
: x(x), y(y)
{
}
double x;
double y;
};
struct line
{
line(double x1, double y1, double x2, double y2)
: p_p1(new point(x1, y1)), p_p2(new point(x2, y2))
{
}
~line() throw()
{
delete this->p_p2;
delete this->p_p1;
}
point *p_p1;
point *p_p2;
};
void f()
{
auto * * array1 = new line *[N];
auto st = std::chrono::steady_clock::now();
for ( auto i = 0; i < N; i++ )
array1[i] = new line(1, 2, 3, 4);
for ( auto i = 0; i < N; i++ )
delete array1[i];
auto et = std::chrono::steady_clock::now();
delete array1;
std::printf("total = %lld ms\n", std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());
std::printf("avg. = %.1f us\n", std::chrono::duration_cast<std::chrono::microseconds>(et - st).count() / static_cast<double>(N) * 1000);
}
int main()
{
for ( auto i = 0; i < 3; i++ )
f();
getchar();
}
/*
C#はこれ
using System;
static class Test
{
const int N = 100 * 1000;
class point
{
public point() { }
public point(double x, double y)
{
this.x = x;
this.y = y;
}
public double x;
public double y;
};
class line
{
public line(double x1, double y1, double x2, double y2)
{
this.p_p1.x = x1;
this.p_p1.y = y1;
this.p_p2.x = x2;
this.p_p2.y = y2;
}
public point p_p1 = new point();
public point p_p2 = new point();
};
static void f()
{
var array1 = new line[N];
var st = DateTime.Now;
for ( var i = 0; i < N; i++ )
array1[i] = new line(1, 2, 3, 4);
array1 = null;
System.GC.Collect(3, GCCollectionMode.Default, true);
var et = DateTime.Now;
Console.WriteLine("total = {0:0.0} ms", (et - st).TotalMilliseconds);
Console.WriteLine("avg. = {0:0.0} ns", (et - st).TotalMilliseconds / (double)N * 1000 * 1000);
}
static void Main()
{
for ( var i = 0; i < 10; i++ )
f();
Console.ReadKey();
}
}
*/
I2luY2x1ZGUgPGNzdGRpbz4gICAgLy8gZm9yIHByaW50Zi4KI2luY2x1ZGUgPGNocm9ubz4JLy8gZm9yIG1pbGxpc2Vjb25kcyw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