Skip to content

Instantly share code, notes, and snippets.

@devyn
Last active July 16, 2024 23:46
Show Gist options
  • Save devyn/f527b51fd2b0d3f52b090ce4bc51e714 to your computer and use it in GitHub Desktop.
Save devyn/f527b51fd2b0d3f52b090ce4bc51e714 to your computer and use it in GitHub Desktop.
Row vs column major tables
Make row major table:
╭──────┬───────────────────╮
│ mean │ 452ms 675µs 108ns │
│ min │ 412ms 552µs 669ns │
│ max │ 525ms 420µs 489ns │
│ std │ 30ms 356µs 567ns │
╰──────┴───────────────────╯
Make column major table:
╭──────┬──────────────────╮
│ mean │ 36ms 469µs 823ns │
│ min │ 35ms 801µs 931ns │
│ max │ 39ms 934µs 92ns │
│ std │ 585µs 439ns │
╰──────┴──────────────────╯
Get single value from row major table:
╭──────┬─────────────────╮
│ mean │ 4ms 107µs 188ns │
│ min │ 4ms 17µs 432ns │
│ max │ 4ms 500µs 273ns │
│ std │ 76µs 611ns │
╰──────┴─────────────────╯
Get single value from col major table:
╭──────┬─────────────────╮
│ mean │ 2ms 21µs 580ns │
│ min │ 1ms 974µs 245ns │
│ max │ 2ms 61µs 822ns │
│ std │ 20µs 551ns │
╰──────┴─────────────────╯
Get row from row major table:
╭──────┬─────────────────╮
│ mean │ 2ms 844µs 363ns │
│ min │ 2ms 782µs 697ns │
│ max │ 3ms 38µs 145ns │
│ std │ 54µs 521ns │
╰──────┴─────────────────╯
Get row from col major table:
╭──────┬─────────────────╮
│ mean │ 1ms 478µs 772ns │
│ min │ 1ms 447µs 180ns │
│ max │ 1ms 546µs 760ns │
│ std │ 21µs 759ns │
╰──────┴─────────────────╯
Row major table to records:
╭──────┬───────────────────╮
│ mean │ 483ms 458µs 820ns │
│ min │ 445ms 47µs 865ns │
│ max │ 535ms 853µs 639ns │
│ std │ 32ms 407µs 150ns │
╰──────┴───────────────────╯
Col major table to records:
╭──────┬───────────────────╮
│ mean │ 500ms 794µs 218ns │
│ min │ 483ms 155µs 609ns │
│ max │ 582ms 724µs 628ns │
│ std │ 22ms 160µs 889ns │
╰──────┴───────────────────╯
#!/usr/bin/env nu
use std bench
def table-to-rowmajor []: table -> record<columns: list<string>, rowmajor_data: list<list>> {
let table = $in
let columns = $table | columns
{
columns: $columns
rowmajor_data: ($table | each { |row|
$columns | each { |col| $row | get $col }
})
}
}
def table-to-colmajor []: table -> record<columns: list<string>, colmajor_data: list<list>> {
let table = $in
let columns = $table | columns
{
columns: $columns
colmajor_data: ($columns | each { |col|
$table | each { |row| $row | get $col }
})
}
}
def table-get [row: int, col: string]: record -> any {
let table = $in
let col_index = $table.columns | enumerate | where item == $col | get index | first
match $table {
{columns: _, rowmajor_data: $data} => {
$data | get $row | get $col_index
}
{columns: _, colmajor_data: $data} => {
$data | get $col_index | get $row
}
}
}
def table-get-row [row: int]: record -> record {
let table = $in
match $table {
{columns: $columns, rowmajor_data: $data} => {
$data |
get $row |
zip $columns |
each { |pair| {name: $pair.1, value: $pair.0} } |
transpose -rd
}
{columns: $columns, colmajor_data: $data} => {
$data |
each { get $row } |
zip $columns |
each { |pair| {name: $pair.1, value: $pair.0} } |
transpose -rd
}
}
}
def table-to-records []: record -> table {
let table = $in
match $table {
{columns: $columns, rowmajor_data: $data} => {
$data |
each {
zip $columns |
each { |pair| {name: $pair.1, value: $pair.0} } |
transpose -rd
}
}
{columns: $columns, colmajor_data: $data} => {
$data |
reduce { |item, acc|
$acc | zip $item | each {|pair| $pair.0 ++ [$pair.1] }
} |
each {
zip $columns |
each { |pair| {name: $pair.1, value: $pair.0} } |
transpose -rd
}
}
}
}
def main [] {
let table = seq 1 10000 |
wrap index |
insert uuid { random uuid } |
insert data { random chars }
print "Make row major table:"
bench { $table | table-to-rowmajor } | reject times | print
print "Make column major table:"
bench { $table | table-to-colmajor } | reject times | print
let table_rowmajor = $table | table-to-rowmajor
let table_colmajor = $table | table-to-colmajor
print "Get single value from row major table:"
bench { $table_rowmajor | table-get 5000 uuid } | reject times | print
print "Get single value from col major table:"
bench { $table_colmajor | table-get 5000 uuid } | reject times | print
print "Get row from row major table:"
bench { $table_rowmajor | table-get-row 5000 } | reject times | print
print "Get row from col major table:"
bench { $table_colmajor | table-get-row 5000 } | reject times | print
print "Row major table to records:"
bench { $table_rowmajor | table-to-records | ignore } | reject times | print
print "Col major table to records:"
bench { $table_colmajor | table-to-records | ignore } | reject times | print
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment