import requests def fetch_data(url): response = requests.get(url) if response.status_code == 200: return response.json() else: return None if __name__ == "__main__": url = "https://jsonplaceholder.typicode.com/posts" data = fetch_data(url) print(data)
package main import ( "encoding/json" "fmt" "net/http" ) type Post struct { UserID int `json:"userId"` ID int `json:"id"` Title string `json:"title"` Body string `json:"body"` } func fetchData(url string) ([]Post, error) { response, err := http.Get(url) if err != nil { return nil, err } defer response.Body.Close() decoder := json.NewDecoder(response.Body) var posts []Post if err := decoder.Decode(&posts); err != nil { return nil, err } return posts, nil } func main() { url := "https://jsonplaceholder.typicode.com/posts" posts, err := fetchData(url) if err != nil { fmt.Println("Error fetching data:", err) return } for _, post := range posts { fmt.Println(post.Title) } }
Original Code | Optimised Code | |
---|---|---|
Go | 46.13 s | 8.34 s |
Python | 2 259.59 s (~ 37 minutes) |
302.37 s (~ 5 minutes) |
# Define a parent class called "Animal" class Animal: def __init__(self, name, age): self.name = name self.age = age def eat(self): print(f"{self.name} is eating.") def sleep(self): print(f"{self.name} is sleeping.") # Define a child class called "Cat" that inherits from "Animal" class Cat(Animal): def __init__(self, name, age, color): super().__init__(name, age) self.color = color def meow(self): print(f"{self.name} says meow.") # Create an instance of the Cat class my_cat = Cat("Whiskers", 3, "gray") # Use the methods my_cat.eat() my_cat.sleep() my_cat.meow()
package main import "fmt" type Animal struct { Name string Age int } // Define methods for the Animal struct func (a *Animal) Eat() { fmt.Printf("%s is eating.\n", a.Name) } func (a *Animal) Sleep() { fmt.Printf("%s is sleeping.\n", a.Name) } // Define a child struct called "Cat" that embeds the Animal struct type Cat struct { Animal Color string } // Define a method for the Cat struct func (c *Cat) Meow() { fmt.Printf("%s says meow.\n", c.Name) } func main() { // Create an instance of the Cat struct myCat := &Cat{ Animal: Animal{ Name: "Whiskers", Age: 3, }, Color: "gray", } // Use the methods myCat.Eat() myCat.Sleep() myCat.Meow() }
package main import ( "fmt" "sync" "time" ) func square(n int, wg *sync.WaitGroup, results chan<- int) { defer wg.Done() fmt.Printf("Calculating square of %d\n", n) time.Sleep(1 * time.Second) results <- n * n // Send the result to the channel } func main() { numbers := []int{1, 2, 3, 4, 5} var wg sync.WaitGroup results := make(chan int) for _, n := range numbers { wg.Add(1) // Add to the WaitGroup go square(n, &wg, results) // Launch goroutine } for result := range results { fmt.Println("Result:", result) } }
import multiprocessing import time # Function to be executed in parallel def square(n): print(f"Calculating square of {n}") time.sleep(1) # Simulate a time-consuming task return n * n if __name__ == "__main__": # Create a list of numbers to compute the square numbers = [1, 2, 3, 4, 5] # Create a pool of workers with multiprocessing.Pool(processes=3) as pool: # Map the function to the numbers list results = pool.map(square, numbers) print("Results:", results)