Skip to content

Instantly share code, notes, and snippets.

View lachlan-eagling's full-sized avatar

Lachlan Eagling lachlan-eagling

  • Sunshine Coast, Australia
View GitHub Profile
@lachlan-eagling
lachlan-eagling / profile_decorator.py
Created November 2, 2019 02:23
Blog - Performance Profiling - Fibonacci Profile
import cProfile
from random import choice
def profiler(func):
def wrapper(*args, **kwargs):
with cProfile.Profile() as pr:
result = func(*args, **kwargs)
pr.print_stats()
return result
@lachlan-eagling
lachlan-eagling / fib_inline_prof.py
Created November 2, 2019 02:06
Blog - Performance Profiling - Fibonacci Profile
import cProfile
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
cProfile.run("fib(35)")
@lachlan-eagling
lachlan-eagling / fib.py
Last active November 2, 2019 01:34
Basic Recursive Fibonacci Sequence
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
fib(35)
@lachlan-eagling
lachlan-eagling / optimised_struct.go
Created October 13, 2019 02:25
Blog - Anatomy of a Struct (Optimised Example)
package main
import (
"fmt"
"unsafe"
)
type OptimisedPost struct {
title string // 16 Bytes
content string // 16 Bytes
@lachlan-eagling
lachlan-eagling / unoptimised_struct.go
Created October 13, 2019 02:23
Blog - Anatomy of a Struct (Unoptimised Example)
package main
import (
"fmt"
"unsafe"
)
type Post struct {
published bool // 1 byte
title string // 16 Bytes
@lachlan-eagling
lachlan-eagling / struct_mem1.go
Last active October 13, 2019 01:59
Blog - Anatomy of a Struct (Basic Struct Memory Example)
package main
import (
"fmt"
"unsafe"
)
type MemoryExample struct {
a int // 8 byte
b int16 // 2 bytes
@lachlan-eagling
lachlan-eagling / unmarshal_users_array.go
Created October 12, 2019 03:00
Blog - Anatomy of a Struct (Unmarshaling Array)
func main() {
var users []User
userJson := []byte(`[{"firstName": "Lachlan", "surname": "Eagling", "username": "Lachlan_E", "age": 28}, {"firstName": "Jon", "surname": "Snow", "username": "Watcher21", "age": 20}]`)
if err := json.Unmarshal(userJson, &users); err != nil {
fmt.Println(err)
}
fmt.Println(users)
}
@lachlan-eagling
lachlan-eagling / unmarshal_user.go
Last active October 26, 2019 12:15
Blog - Anatomy of a Struct (Unmarshaling)
package main
import (
"encoding/json"
"fmt"
)
type User struct {
FirstName string `json:"firstName"`
LastName string `json:"surname"`
@lachlan-eagling
lachlan-eagling / user_instantiation.go
Created October 12, 2019 01:50
Blog - Anatomy of a Struct (Using Struct)
func main() {
user := User{
firstName: "Lachlan",
lastName: "Eagling",
username: "LachlanEagling",
age: 7,
}
if err := user.UpdateUsername("Lachlan_E"); err != nil {
fmt.Println(err)
}
// UpdateUsername updates username if valid, else returns an error.
func (u *User) UpdateUsername(newUsername string) error {
if err := validateUsername(newUsername); err != nil {
return err
}
u.username = newUsername
return nil
}