This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cProfile | |
| def fib(n): | |
| if n < 2: | |
| return n | |
| return fib(n - 1) + fib(n - 2) | |
| cProfile.run("fib(35)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fib(n): | |
| if n < 2: | |
| return n | |
| return fib(n - 1) + fib(n - 2) | |
| fib(35) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "unsafe" | |
| ) | |
| type OptimisedPost struct { | |
| title string // 16 Bytes | |
| content string // 16 Bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "unsafe" | |
| ) | |
| type Post struct { | |
| published bool // 1 byte | |
| title string // 16 Bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "unsafe" | |
| ) | |
| type MemoryExample struct { | |
| a int // 8 byte | |
| b int16 // 2 bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| type User struct { | |
| FirstName string `json:"firstName"` | |
| LastName string `json:"surname"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| user := User{ | |
| firstName: "Lachlan", | |
| lastName: "Eagling", | |
| username: "LachlanEagling", | |
| age: 7, | |
| } | |
| if err := user.UpdateUsername("Lachlan_E"); err != nil { | |
| fmt.Println(err) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
NewerOlder