Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune function rangeI #245

Open
wangkuiyi opened this issue Aug 28, 2020 · 0 comments
Open

Tune function rangeI #245

wangkuiyi opened this issue Aug 28, 2020 · 0 comments
Assignees

Comments

@wangkuiyi
Copy link
Owner

The current definition is here

func rangeI(n int64) []int64 {
res := []int64{}
if n <= 0 {
return res
}
for i := int64(0); i < n; i++ {
res = append(res, i)
}
return res
}

I noticed some issues with this function.

  1. The frequent call to append might be expensive. Each call might deallocate and reallocate and copy existing slice data. Instead, we can make([]int64, n), then copy each generated element to the right place.

  2. This function would crash the program if the parameter n is too large. It would be safer to add a check to panic if n is larger than a threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants