From 63f04f004110ecabfb5ec32408e77055a62a9122 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Mon, 23 Oct 2023 14:01:09 -0400 Subject: Recommend using WAL for 100% better performance --- README.md | 6 +++--- test.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 72ae70c..e221ed5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a C port of [SD](https://git.exozy.me/a/SD), a very efficient (and a tin ## Usage -Flash cards are stored in the `cards` table of a SQLite database. There are four columns: `idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING`. The `idx` is a unique index for each card, starting at 0. The weight is how often the card should come up. The key and value are the front and reverse sides of the card. You can use the `sqlite3` CLI to create a card deck. +Flash cards are stored in the `cards` table of a SQLite database. There are four columns: `idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING`. The `idx` is a unique index for each card, starting at 0. The weight is how often the card should come up. The key and value are the front and reverse sides of the card. You can use the `sqlite3` CLI to create a card deck. You may be able to get twice as fast performance by enabling WAL with `PRAGMA journal_mode=WAL` because WAL only writes the content once instead of twice. Now build this project with `gcc sd.c segmenttree.c -o sd -lsqlite3 -O2 -march=native` and run `./sd` to enjoy a fast flash cards experience! The program will display the `key` of a randomly selected card. Press any key to show the `val` of the card. Now press either `y` or `n` depending on whether you got the card correct, and the program adjusts that card's weight. @@ -14,7 +14,7 @@ If you're wondering where the name came from, this is the C port of [SD](https:/ SD is designed to be extremely efficient and [supports decks with hundreds of millions of flash cards](test.py). If `N` is the number of cards, initializing the program requires `O(N)` time and `O(N)` memory. Selecting a random card and adjusting its weight requires `O(log N)` time. Internally SD uses [segment trees](https://en.wikipedia.org/wiki/Segment_tree) to achieve this time complexity. -Some benchmark results using 10 card updates: +Some benchmark results using 10 card updates (without WAL): ``` C version: ./sd < test Time (mean ± σ): 57.4 ms ± 4.5 ms [User: 6.9 ms, System: 2.4 ms] @@ -24,4 +24,4 @@ Go version: ./sd < test Range (min … max): 79.4 ms … 108.9 ms 33 runs ``` -The C port is about 30% faster than the original Go code. \ No newline at end of file +The C port is about 30% faster than the original Go code. diff --git a/test.py b/test.py index 821bf62..451b403 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,7 @@ import sqlite3 con = sqlite3.connect("test.db") +con.execute("PRAGMA journal_mode=wal") cur = con.cursor() cur.execute("CREATE TABLE IF NOT EXISTS cards (idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING)") for i in range(10**8): -- cgit v1.2.3-70-g09d2