Passing values vs pointers in Go - Performance Comparison
When we pass values or pointers in Go, it affects how much work the program does.
We want to see how the time to run changes as the data size grows.
Analyze the time complexity of the following code snippet.
func processValue(data [1000]int) {
// Simulate some processing
for _, v := range data {
_ = v * 2
}
}
func processPointer(data *[1000]int) {
// Simulate some processing
for _, v := range *data {
_ = v * 2
}
}
This code shows two functions: one takes a large array by value, the other by pointer, both loop over the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over 1000 elements in the array.
- How many times: Exactly 1000 times for each function call.
- Additional operation for value passing: Copying the entire array once when calling the function.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Copy 10 items + loop 10 times |
| 100 | Copy 100 items + loop 100 times |
| 1000 | Copy 1000 items + loop 1000 times |
Pattern observation: Passing by value adds a copying step that grows with input size, while passing by pointer avoids this copy.
Time Complexity: O(n)
This means the time grows linearly with the size of the data, mainly due to looping and copying when passing by value.
[X] Wrong: "Passing by pointer always makes the code faster regardless of what happens inside the function."
[OK] Correct: Sometimes the function does more work that dominates time, so the cost of copying might be small compared to processing.
Understanding how passing values or pointers affects performance helps you write efficient code and explain your choices clearly.
"What if the array size doubled? How would passing by value vs pointer affect the time complexity then?"
Practice
Solution
Step 1: Understand passing by value
Passing by value means the function gets a copy of the variable, not the original.Step 2: Effect of changes inside the function
Changes to the copy do not affect the original variable outside the function.Final Answer:
The function receives a copy; changes inside do not affect the original variable. -> Option AQuick Check:
Passing by value = copy, no original change [OK]
- Thinking changes inside function affect original variable
- Confusing pointers with values
- Assuming function cannot modify any data
Solution
Step 1: Recall pointer syntax in Go function parameters
In Go, a pointer parameter is declared by placing * before the type, e.g., *int.Step 2: Identify correct syntax
func updateValue(val *int) {} is correct syntax, with * before the type. func updateValue(val int) {} declares a value parameter. func updateValue(&val int) {} and func updateValue(*val int) {} have invalid syntax.Final Answer:
func updateValue(val *int) {} -> Option AQuick Check:
Pointer parameter syntax = *type [OK]
- Putting * before variable name instead of type
- Using & in parameter declaration
- Confusing pointer syntax with value parameter
package main
import "fmt"
func changeValue(val int) {
val = 10
}
func main() {
x := 5
changeValue(x)
fmt.Println(x)
}Solution
Step 1: Analyze function parameter passing
The function changeValue receives x by value, so val is a copy of x.Step 2: Check if original variable changes
Inside changeValue, val is set to 10, but this does not affect x in main.Final Answer:
5 -> Option BQuick Check:
Passing by value means original x stays 5 [OK]
- Assuming x changes to 10 after function call
- Confusing pointer and value passing
- Expecting compilation error due to assignment
func increment(num *int) {
num = num + 1
}
func main() {
x := 5
increment(&x)
fmt.Println(x)
}Solution
Step 1: Understand pointer arithmetic in Go
Go does not support arithmetic on pointers like 'num + 1'. You must dereference the pointer to change the value.Step 2: Correct way to increment value pointed by pointer
Use '*num = *num + 1' to increment the value at the pointer address.Final Answer:
Cannot add 1 to a pointer directly; need to dereference first. -> Option DQuick Check:
Pointer arithmetic invalid; dereference to modify [OK]
- Trying to add directly to pointer variable
- Forgetting to dereference pointer
- Confusing pointer and value types
Solution
Step 1: Understand how to modify original variable
Passing by value copies the variable, so changes inside function won't affect original.Step 2: Use pointer to modify original variable
Passing a pointer allows the function to access and change the original variable's value via dereferencing.Step 3: Consider alternatives
Returning doubled value requires assignment outside; using global variables is bad practice.Final Answer:
Pass a pointer to the integer and modify the value via dereferencing. -> Option CQuick Check:
Pointer passing modifies original variable [OK]
- Assuming passing by value changes original variable
- Relying on return value without assignment
- Using global variables unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
