-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (47 loc) · 1.74 KB
/
Copy pathProgram.cs
File metadata and controls
56 lines (47 loc) · 1.74 KB
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
47
48
49
50
51
52
53
54
55
56
namespace stringsearch;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Initializing...");
var wordList = new WordList();
var trie = new PrefixTree();
Stopwatch trieInsertionStopwatch = Stopwatch.StartNew();
trie.InsertWordList(wordList);
trieInsertionStopwatch.Stop();
Console.WriteLine("Type a prefix and press return.");
while (true)
{
var query = Console.ReadLine()?.ToUpper();
Console.WriteLine();
if (string.IsNullOrEmpty(query))
{
continue;
}
Stopwatch plinqStopwatch = Stopwatch.StartNew();
var plinqResult = wordList.Search(query);
plinqStopwatch.Stop();
Stopwatch trieStopwatch = Stopwatch.StartNew();
var trieResult = await trie.Search(query);
trieStopwatch.Stop();
if (trieResult?.ToList().Count > 0)
{
foreach (var word in trieResult)
{
Console.WriteLine(word);
}
Console.WriteLine($"\nSearch results: {trieResult?.ToList().Count}");
Console.WriteLine($"PLINQ search time: {plinqStopwatch.ElapsedMilliseconds} ms / {plinqStopwatch.ElapsedTicks} ticks");
Console.WriteLine($"Trie search time: {trieStopwatch.ElapsedMilliseconds} ms / {trieStopwatch.ElapsedTicks} ticks (+ {trieInsertionStopwatch.ElapsedMilliseconds} ms for insertion)\n");
}
else
{
Console.WriteLine("Prefix not found.\n");
}
}
}
}