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

Improved: arrayqueue dequeue method #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yashwanth-reddy909
Copy link

@yashwanth-reddy909 yashwanth-reddy909 commented Nov 3, 2024

improved dequeue method of arrayqueue

  • improved the time complexity of ArrayQueueDequeue from O(n) to O(1)
  • observed huge optimisation on BenchmarkArrayQueueDequeue100000 by over 2757% time for each operation

New: Benchmarks of ArrayQueueDequeue using RemoveFirstNElements

goos: linux
goarch: amd64
pkg: github.com/emirpasic/gods/v2/queues/arrayqueue
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkArrayQueueDequeue100-20         4909561           237.7 ns/op
BenchmarkArrayQueueDequeue1000-20         520776          2308 ns/op
BenchmarkArrayQueueDequeue10000-20         52116         23009 ns/op
BenchmarkArrayQueueDequeue100000-20         5180        230320 ns/op
PASS
ok      github.com/emirpasic/gods/v2/queues/arrayqueue  13.360s

Existing: Benchmarks of ArrayQueueDequeue using Remove

goos: linux
goarch: amd64
pkg: github.com/emirpasic/gods/v2/queues/arrayqueue
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkArrayQueueDequeue100-20         5031644           237.7 ns/op
BenchmarkArrayQueueDequeue1000-20         517448          2305 ns/op
BenchmarkArrayQueueDequeue10000-20         60136         23511 ns/op
BenchmarkArrayQueueDequeue100000-20            2     635077017 ns/op
PASS
ok      github.com/emirpasic/gods/v2/queues/arrayqueue  14.776s

when the array queue size increases, numbers would look even better

Copy link

sonarcloud bot commented Nov 3, 2024

@yashwanth-reddy909
Copy link
Author

yashwanth-reddy909 commented Nov 3, 2024

existing dequeue method calling Remove(0) function of arraylist which removes the zero index element of the slice using slices.Delete which it does is

s = append(s[:i], s[j:]...)

updated it to this s = s[1:]

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

Successfully merging this pull request may close these issues.

1 participant