Skip to content

Instantly share code, notes, and snippets.

func BenchmarkParseWithStringManipulation(b *testing.B) {
for n := 0; n < b.N; n++ {
parseWithStringManipulation("7PM")
}
}
func parseWithStringManipulation(inputString string) int {
isAm := strings.HasSuffix(inputString, "AM")
number, _ := strconv.Atoi(inputString[:len(inputString) - 2])
if !isAm {
number += 12
}
return number
}
func BenchmarkParseWithReplaceAllTrimSuffix(b *testing.B) {
for n := 0; n < b.N; n++ {
parseWithReplaceAllTrimSuffix("7PM")
}
}
func parseWithReplaceAllTrimSuffix(inputString string) int {
isAm := strings.HasSuffix(inputString, "AM")
if isAm {
inputString = strings.TrimSuffix(inputString, "AM")
} else {
inputString = strings.TrimSuffix(inputString, "PM")
}
number, _ := strconv.Atoi(inputString)
func BenchmarkParseWithReplaceAll(b *testing.B) {
for n := 0; n < b.N; n++ {
parseWithReplaceAll("7PM")
}
}
func parseWithReplaceAll(inputString string) int {
isAm := strings.HasSuffix(inputString, "AM")
if isAm {
inputString = strings.ReplaceAll(inputString, "AM", "")
} else {
inputString = strings.ReplaceAll(inputString, "PM", "")
}
number, _ := strconv.Atoi(inputString)
package main
import (
"testing"
)
func BenchmarkParseWithDateTime(b *testing.B) {
for n := 0; n < b.N; n++ {
parseWithDateTime("7PM")
}
@karakanb
karakanb / naive.go
Last active December 15, 2020 17:56
func parseWithDateTime(inputString string) int {
t, _ := time.Parse("3PM", inputString)
number, _ := strconv.Atoi(t.Format("15"))
return number
}
@karakanb
karakanb / docker-compose.yml
Last active November 2, 2020 00:04
simple pi-hole docker-compose setup
version: "3.8"
services:
pihole:
container_name: pihole
image: pihole/pihole:v5.1.2
restart: always
environment:
TZ: 'Europe/Berlin' # Put your own timezone here.
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class PlansController extends Controller
{