Sobes.tech
Junior — Senior

Accelerating the execution of a large number of network requests

livecode

Task condition

It is necessary to increase the execution speed of the networkRequest() function, which simulates a network request by delaying execution with time.Sleep(time.Millisecond) and increments the global counter count.

import (
        "fmt"
        "time"
        )

const numRequests = 1000

var count int

func networkRequest() {
    time.Sleep(time.Millisecond)

    count++;
}

func main() {
    defer timer() ()

    for i := 0 ; i < numRequests; i++ {
        networkRequest()
    }
}

func timer() func () {
    start :=time.Now()

    return func(){
        fmt.Println("count %v took %v\n", count, time.Since(start))
       }
}