Skip to main content

Azure Functions: Merge Algorithm Runtime Comparison (Part 1)

Keith Hodo
Author
Keith Hodo
Solutions Architect at AWS. Writing about cloud, agentic AI, and the journey.

Introduction
#

One of the things I really enjoy while writing variations of algorithms is comparing the total run-time between them. I find it rewarding to start with a minimal amount of iterations, say 10 merges, where QuickFind (O(n²)) and WeightedQuickUnion (O(m log n)) look almost the same. Then add more iterations to see the time savings become apparent.

For today’s demo we compare four algorithms from Robert Sedgewick’s Algorithms in Java: QuickFind, QuickUnion, WeightedQuickUnion, and WeightedQuickUnionWithPathCompression.

The architecture: an HTTPTrigger writes a message to a Service Bus Topic with Subscriptions for each algorithm. Each algorithm runs as a ServiceBusTopicTrigger, writes output to Blob Storage and performance data to Table Storage.

Prerequisites
#

Adding the Topic
#

Create a Service Bus namespace in the Azure Portal, grab the connection string from Shared Access Policies, and add it as AzureWebJobsServiceBus in your Function App settings.

Creating The ServiceBus
ServiceBus Setting Up Namespace
Retrieving Our ServiceBus Connection String
Copy the ServiceBus Connection String
Our Functions App Settings
Adding the ServiceBus endpoint to our Function App

Adding the Code
#

The MergeTrigger HTTP Trigger receives the arrays, creates the ServiceBus Topic and Subscriptions (one per algorithm), and sends the message. The refactored MergingArray.cs contains all four algorithms with a Merge() method that dispatches based on an enum.

Postman Setup
Postman Setup 2
Postman MergeTrigger
Postman MergeTrigger
ServiceBus: Waiting in Line

Conclusion
#

Today we set up ServiceBus Topics so one HTTP request fans out to four algorithm subscriptions. Part 2 converts our HTTP Trigger to a ServiceBus Trigger. Part 3 adds the remaining algorithms and analyzes performance.