Skip to content

Instantly share code, notes, and snippets.

@ryandesign
Created September 28, 2022 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandesign/d81e92292ea00ba16eb4f633f1fdc5be to your computer and use it in GitHub Desktop.
Save ryandesign/d81e92292ea00ba16eb4f633f1fdc5be to your computer and use it in GitHub Desktop.
Retro68 program demonstrating poor performance of NewPtr
0 2 ..
1 3 ...
2 7 .......
3 8 ........
4 11 ...........
5 13 .............
6 15 ...............
7 17 .................
8 20 ....................
9 22 ......................
#include <Memory.h>
#include <OSUtils.h>
#include <memory>
#include <stdio.h>
int const num_ptrs = 150;
class test_obj
{
public:
test_obj()
{
for (int i = 0; i < num_ptrs; ++i)
p[i] = NewPtr(4);
}
~test_obj()
{
for (int i = 0; i < num_ptrs; ++i)
if (p[i])
DisposePtr(p[i]);
}
private:
Ptr p[num_ptrs];
};
int const num_objs = 10;
std::shared_ptr<test_obj> objs[num_objs];
#define OUTPUT_TO_FILE 0
#if OUTPUT_TO_FILE
FILE *fp;
#define OUTPUT(...) do {fprintf(fp, __VA_ARGS__);} while (0)
#else
#define OUTPUT(...) do {printf(__VA_ARGS__);} while (0)
#endif
void make_obj(int obj_num)
{
unsigned long time_start, time_end, time_diff;
time_start = TickCount();
objs[obj_num] = std::make_shared<test_obj>();
time_end = TickCount();
time_diff = time_end - time_start;
OUTPUT("%4d %4ld ", obj_num, time_diff);
int num_dots = time_diff;
for (int dot = 0; dot < num_dots; ++dot)
OUTPUT(".");
OUTPUT("\n");
}
void clear_obj(int obj_num)
{
objs[obj_num] = nullptr;
}
int main(int argc, char **argv)
{
int obj_num;
(void)argc;
(void)argv;
#if OUTPUT_TO_FILE
if ((fp = fopen("out", "w")))
{
#endif
for (obj_num = 0; obj_num < num_objs; ++obj_num)
make_obj(obj_num);
/*
for (obj_num = 0; obj_num < num_objs; ++obj_num)
clear_obj(obj_num);
for (obj_num = 0; obj_num < num_objs; ++obj_num)
make_obj(obj_num);
clear_obj(2);
make_obj(2);
clear_obj(2);
make_obj(2);
clear_obj(6);
make_obj(6);
clear_obj(6);
make_obj(6);
*/
#if OUTPUT_TO_FILE
fclose(fp);
}
#else
getchar();
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment