Este concepto creo que tambien se llama memoization.
https://es.wikipedia.org/wiki/Memoización
Introducción
Cómo aprender Go avanzado: concurrencia, patrones de diseño y net
Concurrencia
Race condition: el problema de depositar y retirar
Sync Mutex: Lock y Unlock
Mutex de lectura y escritura
Sistema de caché sin concurrencia
Sistema de caché con concurrencia
Reutilización de computación intensiva
Patrones de diseño
¿Qué son los patrones de diseño?
Factory
Singleton
Adapter
Observer
Strategy
Net
Escaneador de puertos sin concurrencia
Escaneador de puertos con concurrencia
Netcat
Servidor de chat en Go con net
Terminando el chat
Conclusión
Resumen del curso de Go avanzado
Aún no tienes acceso a esta clase
Crea una cuenta y continúa viendo este curso
Aportes 3
Preguntas 1
Este concepto creo que tambien se llama memoization.
https://es.wikipedia.org/wiki/Memoización
Use este Printf
en el main
para que fueran más claros los resultados que se muestran:
fmt.Printf("Calculate: %d, Time: %s, Result: %d\n", n, time.Since(start), value)
Mi codigo con comentarios
package main
import (
"fmt"
"time"
)
// Function to calculate fibonacci
func Fibonacci(n int) int {
if n <= 1 {
return n
}
return Fibonacci(n-1) + Fibonacci(n-2)
}
// Memory holds a function and a map of results
type Memory struct {
f Function // Function to be used
cache map[int]FunctionResult // Map of results for a given key
}
// A function has to recive a value and return a value and an error
type Function func(key int) (interface{}, error)
// The result of a function
type FunctionResult struct {
value interface{}
err error
}
// NewCache creates a new cache
func NewCache(f Function) *Memory {
return &Memory{f, make(map[int]FunctionResult)}
}
// Get returns the value for a given key
func (m *Memory) Get(key int) (interface{}, error) {
// Check if the value is in the cache
res, exist := m.cache[key]
// If the value is not in the cache, calculate it
if !exist {
res.value, res.err = m.f(key) // Calculate the value
m.cache[key] = res // Store the value in the cache
}
return res.value, res.err
}
// Function to be used in the cache
func GetFibonacci(key int) (interface{}, error) {
return Fibonacci(key), nil
}
func main() {
// Create a cache and some values
cache := NewCache(GetFibonacci)
values := []int{42, 40, 41, 42, 38, 41}
// For each value to calculate, get the value and print the time it took to calculate
for _, v := range values {
start := time.Now()
value, err := cache.Get(v)
if err != nil {
panic(err)
}
fmt.Printf("%d: %d t: %s \n", v, value, time.Since(start))
}
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.