Performance - FunctionCall vs Event vs Action vs Delegate

Posted by hwcverwe on Stack Overflow See other posts from Stack Overflow or by hwcverwe
Published on 2012-11-28T12:53:27Z Indexed on 2012/11/29 11:05 UTC
Read the original article Hit count: 191

Currently I am using Microsoft Sync Framework to synchronize databases. I need to gather information per record which is inserted/updated/deleted by Microsoft Sync Framework and do something with this information.

The sync speed can go over 50.000 records per minute. So that means my additional code need to be very lightweight otherwise it will be a huge performance penalty.

Microsoft Sync Framework raises an SyncProgress event for each record. I am subscribed to that code like this:

// Assembly1
SyncProvider.SyncProgress += OnSyncProgress;
// ....
private void OnSyncProgress(object sender, DbSyncProgressEventArgs e)
{
    switch (args.Stage)
    {
        case DbSyncStage.ApplyingInserts:
            // MethodCall/Delegate/Action<>/EventHandler<> => HandleInsertedRecordInformation 
            // Do something with inserted record info
            break;
        case DbSyncStage.ApplyingUpdates:
            // MethodCall/Delegate/Action<>/EventHandler<> => HandleUpdatedRecordInformation  
            // Do something with updated record info
            break;
        case DbSyncStage.ApplyingDeletes:
            // MethodCall/Delegate/Action<>/EventHandler<> => HandleDeletedRecordInformation
            // Do something with deleted record info
            break;
    }
}

Somewhere else in another assembly I have three methods:

// Assembly2
public class SyncInformation
{
    public void HandleInsertedRecordInformation(...) {...}
    public void HandleUpdatedRecordInformation(...) {...}
    public void HandleInsertedRecordInformation(...) {...}
}

Assembly2 has a reference to Assembly1. So Assembly1 does not know anything about the existence of the SyncInformation class which need to handle the gathered information. So I have the following options to trigger this code:

  1. use events and subscribe on it in Assembly2
    1.1. EventHandler<>
    1.2. Action<>
    1.3. Delegates
  2. using dependency injection:
    public class Assembly2.SyncInformation : Assembly1.ISyncInformation
  3. Other?

I know the performance depends on:

  • OnSyncProgress
    • switch
    • using a method call, delegate, Action<> or EventHandler<>
  • Implementation of SyncInformation class

I currently don't care about the implementation of the SyncInformation class. I am mainly focused on the OnSyncProgress method and how to call the SyncInformation methods.

So my questions are:

  • What is the most efficient approach?
  • What is the most in-efficient approach?
  • Is there a better way than using a switch in OnSyncProgress?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance