File: hashmap_gc_test.d

package info (click to toggle)
dcontainers 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: makefile: 74
file content (46 lines) | stat: -rw-r--r-- 964 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import containers : HashMap;
import std.stdio : writefln;
import core.memory : GC;

/**
 * Generate a random alphanumeric string.
 */
@trusted string randomString(uint len)
{
	import std.ascii : letters, digits;
	import std.conv : to;
	import std.random : randomSample;
	import std.range : chain;

	auto asciiLetters = to!(dchar[])(letters);
	auto asciiDigits = to!(dchar[])(digits);

	if (len == 0)
		len = 1;

	auto res = to!string(randomSample(chain(asciiLetters, asciiDigits), len));
	return res;
}

void main()
{
	immutable iterationCount = 4;
	HashMap!(string, string) hmap;

	for (uint n = 1; n <= iterationCount; n++)
	{
		foreach (i; 0 .. 1_000_000)
			hmap[randomString(4)] = randomString(16);
		GC.collect();
		hmap = HashMap!(string, string)(16);
		GC.collect();

		foreach (i; 0 .. 1_000_000)
			hmap[randomString(4)] = randomString(16);
		GC.collect();
		hmap.clear();
		GC.collect();

		writefln("iteration %s/%s finished", n, iterationCount);
	}
}