-
-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding mentoring notes for cars-assemble question
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
tracks/go/exercises/cars-assemble/cars-assemble/mentoring.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Initial Notes | ||
- Both CalculateWorkingCarsPerHour and CalculateWorkingCarsPerMinute do not need to have multiple variables declared instead they can just be calculated in the return statement. | ||
- Both the 95000 and 10000 constants that are brought in from the question prompt should be declared as constants either in the CalculateCost function or in the package itself, this avoids a "MagicNumber" and allows you to declare a name for the values. | ||
|
||
# Benchmark code | ||
|
||
```golang | ||
package cars | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
// Benchmark for CalculateWorkingCarsPerHour using a table-driven approach | ||
func BenchmarkCalculateWorkingCarsPerHour(b *testing.B) { | ||
tests := []struct { | ||
productionPerHour int | ||
successRate float64 | ||
}{ | ||
{1547, 90}, | ||
{2000, 75}, | ||
{3000, 50}, | ||
{1000, 100}, | ||
{500, 0}, | ||
} | ||
|
||
for _, tt := range tests { | ||
b.Run( | ||
// Name the sub-benchmark using the input values | ||
"productionPerHour="+strconv.Itoa(tt.productionPerHour)+"-successRate="+strconv.FormatFloat(tt.successRate, 'f', -1, 64), | ||
func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
CalculateWorkingCarsPerHour(tt.productionPerHour, tt.successRate) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Benchmark for CalculateWorkingCarsPerMinute using a table-driven approach | ||
func BenchmarkCalculateWorkingCarsPerMinute(b *testing.B) { | ||
tests := []struct { | ||
productionPerHour int | ||
successRate float64 | ||
}{ | ||
{1105, 90}, | ||
{2000, 75}, | ||
{1500, 50}, | ||
{800, 100}, | ||
{600, 0}, | ||
} | ||
|
||
for _, tt := range tests { | ||
b.Run( | ||
"productionPerHour="+strconv.Itoa(tt.productionPerHour)+"-successRate="+strconv.FormatFloat(tt.successRate, 'f', -1, 64), | ||
func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
CalculateWorkingCarsPerMinute(tt.productionPerHour, tt.successRate) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Benchmark for CalculateCost using a table-driven approach | ||
func BenchmarkCalculateCost(b *testing.B) { | ||
tests := []struct { | ||
carsCount int | ||
}{ | ||
{37}, | ||
{21}, | ||
{100}, | ||
{250}, | ||
{1000}, | ||
{5000}, | ||
} | ||
|
||
for _, tt := range tests { | ||
b.Run( | ||
"carsCount="+strconv.Itoa(tt.carsCount), | ||
func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
CalculateCost(tt.carsCount) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
``` |