Skip to content

Instantly share code, notes, and snippets.

Avatar

Lachlan Eagling lachlan-eagling

View GitHub Profile
@lachlan-eagling
lachlan-eagling / profile_decorator.py
Created November 2, 2019 02:23
Blog - Performance Profiling - Fibonacci Profile
View profile_decorator.py
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
View fib_inline_prof.py
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_prof.py
Last active November 2, 2019 01:33
Blog - Performance Profiling - Fibonacci Profile
View fib_prof.py
29860705 function calls (3 primitive calls) in 5.973 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 5.973 5.973 profiling.py:1(<module>)
29860703/1 5.973 0.000 5.973 5.973 profiling.py:1(fib)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
@lachlan-eagling
lachlan-eagling / fib.py
Last active November 2, 2019 01:34
Basic Recursive Fibonacci Sequence
View fib.py
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)
View optimised_struct.go
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)
View unoptimised_struct.go
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)
View struct_mem1.go
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)
View unmarshal_users_array.go
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)
View unmarshal_user.go
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)
View user_instantiation.go
func main() {
user := User{
firstName: "Lachlan",
lastName: "Eagling",
username: "LachlanEagling",
age: 7,
}
if err := user.UpdateUsername("Lachlan_E"); err != nil {
fmt.Println(err)
}