Skip to content

Improved: arrayqueue dequeue method#267

Open
yashwanth-reddy909 wants to merge 1 commit into
emirpasic:masterfrom
yashwanth-reddy909:queue-optimazation
Open

Improved: arrayqueue dequeue method#267
yashwanth-reddy909 wants to merge 1 commit into
emirpasic:masterfrom
yashwanth-reddy909:queue-optimazation

Conversation

@yashwanth-reddy909

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

Copy link
Copy Markdown

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

@sonarqubecloud

sonarqubecloud Bot commented Nov 3, 2024

Copy link
Copy Markdown

@yashwanth-reddy909

yashwanth-reddy909 commented Nov 3, 2024

Copy link
Copy Markdown
Author

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:]

@yashwanth-reddy909

Copy link
Copy Markdown
Author

@emirpasic please go through this MR. High improvement on the Queue DS.

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