C port of SD, a very efficient flash cards app
Go to file
Anthony Wang db359b424a
Mention sdc-git AUR package in README
2024-01-30 22:48:44 -05:00
LICENSE Initial commit 2022-04-18 20:03:31 -05:00
README.md Mention sdc-git AUR package in README 2024-01-30 22:48:44 -05:00
main.py Use sd binary in same directory as scripts instead of needing to pass path to sd binary 2024-01-30 21:59:19 -05:00
sd-add.fish Don't require -f flag for specifying file (adding -f won't hurt though), allow specifying sd-add card file as argv[1] 2024-01-30 22:42:49 -05:00
sd.c Don't require -f flag for specifying file (adding -f won't hurt though), allow specifying sd-add card file as argv[1] 2024-01-30 22:42:49 -05:00
segmenttree.c Add missing space 2023-03-17 00:37:37 +00:00
segmenttree.h Finish porting SD code 2022-04-19 12:54:07 -05:00
sqlitevi.fish Add command for creating table in README, add fish helper scripts 2023-12-29 16:16:25 -06:00
test.py Use sd binary in same directory as scripts instead of needing to pass path to sd binary 2024-01-30 21:59:19 -05:00
tkinter.py Use sd binary in same directory as scripts instead of needing to pass path to sd binary 2024-01-30 21:59:19 -05:00

SDC

This is a C port of SD, a very efficient (and a tiny bit overengineered) flash cards app. SDC is finished, which means that no new features will be added, only bug fixes.

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.

To create a card deck, use sqlite3 cards "CREATE TABLE cards (idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING). You may be able to double the performance by enabling WAL with PRAGMA journal_mode=WAL, because WAL only writes the content once instead of twice.

Now build SDC with gcc sd.c segmenttree.c -o sd -lsqlite3 -O2 -march=native and run ./sd to enjoy a fast flash cards experience! You can also install the sdc-git package from the AUR. 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.

There is also a Python GUI, main.py, which requires PyQt6. This script invokes the sd binary in the same directory as the script with the command-line flags that were passed to the script. Alternatively, there is a Tkinter GUI in tkinter.py that only requires the Python standard library but does not support Wayland. The GUIs are not compatible with the original unmaintained SD which lacks noninteractive mode, but it would be easy to add that feature to SD.

It should also be fairly easy to write your own SD clone or GUI, so if you write one, I'd be glad to link to it here.

If you're wondering where the name came from, this is the C port of SD, which was named after a common type of flash card. 😀

Performance

SD is designed to be extremely efficient and supports decks with hundreds of millions of flash cards. 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 to achieve this time complexity.

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]
  Range (min … max):    51.9 ms …  70.9 ms    41 runs
Go version: ./sd < test
  Time (mean ± σ):      92.7 ms ±   6.8 ms    [User: 8.4 ms, System: 4.5 ms]
  Range (min … max):    79.4 ms … 108.9 ms    33 runs

The C port is about 30% faster than the original Go code.

Tips and tricks

  • View card deck: sqlite3 cards .dump
  • Get total number of cards: sqlite3 cards "SELECT COUNT(*) FROM cards"
  • Get total number of cards with positive weight: sqlite3 cards "SELECT COUNT(*) FROM cards WHERE weight>0"
  • Search for string in keys: sqlite3 cards "SELECT * FROM cards WHERE key LIKE '%hello%'"
  • Add card to deck: sd-add.fish
  • Edit deck: sqlitevi.fish or sqlitebrowser