← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 9 of 10

Benchmarks लिखना

एक benchmark आपके code के लिए एक timed race है -- Go इसके एक हिस्से को बार-बार चलाता है और report करता है कि यह बिल्कुल कितना तेज़ (या धीमा) है।
Syntax
go
func BenchmarkName(b *testing.B) {
    for i := 0; i < b.N; i++ {
        // code to measure
    }
}

Anatomy of a Benchmark Function

एक benchmark function measured हो रहे code को b.N बार दोहराता है, जहां Go का testing framework एक stable timing measurement पाने के लिए N को अपने आप काफी बड़ा चुनता है -- यहां एक manually looped simulation के रूप में दिखाया गया।

Note: असली benchmarks _test.go files में रहते हैं और 'go test -bench=.' से चलते हैं; यह यहां चलने के लिए main में वही b.N loop pattern simulate करता है।

उदाहरण: Anatomy of a Benchmark Function

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function fibonacci
func fibonacci(n int) int {
	// Check whether n < 2
	if n < 2 {
		// Send n back to the caller
		return n
	}
	// Short declaration: create a, b and infer the type
	a, b := 0, 1
	// Loop: repeat while the condition holds
	for i := 2; i <= n; i++ {
		a, b = b, a+b
	}
	// Send b back to the caller
	return b
}

func main() {
	const simulatedN = 1000
	for i := 0; i < simulatedN; i++ {
		fibonacci(20)
	}
	fmt.Println("ran fibonacci(20) simulatedN times:", simulatedN)
	fmt.Println("result:", fibonacci(20))
}

Excluding Setup from Timing

जब किसी benchmark को loop से पहले महंगे setup की ज़रूरत हो (जैसे test data बनाना), उस setup के ठीक बाद b.ResetTimer() call करना इसे measured time से बाहर रखता है, ताकि सिर्फ test किए जा रहे code का असल में समय लिया जाए।

उदाहरण: Excluding Setup from Timing

markup
package main

import "fmt"

func sumSlice(nums []int) int {
	total := 0
	for _, n := range nums {
		total += n
	}
	return total
}

func main() {
	// setup (would be excluded from timing via b.ResetTimer() in a real benchmark)
	data := make([]int, 1000)
	for i := range data {
		data[i] = i
	}

	// timed portion
	result := sumSlice(data)
	fmt.Println("sum:", result)
}

Running Benchmarks

'go test -bench=.' किसी package में हर BenchmarkXxx function चलाता है, कितनी iterations चलीं और प्रति operation average time report करते हुए, जो changes में performance compare करने का standard तरीका है।

उदाहरण: Running Benchmarks

bash
go test -bench=.

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी benchmark function को exact Benchmark prefix के बिना नाम देना, इसलिए 'go test -bench' इसे discover नहीं करेगा।
  2. benchmark के अंदर test किए जा रहे code को b.N बार loop करना भूल जाना -- Go एक stable measurement पाने के लिए N अपने आप तय करता है।
  3. timed loop के अंदर एक-बार का setup काम शामिल करना बजाय setup के बाद b.ResetTimer() से timer reset करने के।
चैप्टर सारांश
  • Benchmark functions BenchmarkXxx नाम के होते हैं, एक *testing.B लेते हैं, और _test.go files में रहते हैं।
  • Measured किए जा रहे code को b.N तक एक loop में चलना चाहिए, एक count जिसे Go का testing framework अपने आप adjust करता है।
  • 'go test -bench=.' benchmarks चलाता है और operations per second और प्रति operation time report करता है।
  • b.ResetTimer() किसी benchmark के timed हिस्से से setup काम को बाहर रखता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.