0

I'm trying to replace this for-each loop with Linq query. This loop is killing the performance. str contains list<string>. I have to read one by one each string from str and encode it then I need to publish it to the server using AMQ.

foreach (var item in str)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(item);
        publisher.SendMessage(bytes);
    }
NetMage
  • 26,163
  • 3
  • 34
  • 55
peter s
  • 9
  • 5
  • 8
    And what makes you think using linq would improve performance? – Rafalon Apr 20 '18 at 13:12
  • 2
    Your IO-bound operation here is `SendMessage()`. Converting to LINQ won't affect that. – Dan Wilson Apr 20 '18 at 13:12
  • Maybe you should consider using async – Th0rndike Apr 20 '18 at 13:13
  • If you use LINQ , you'll get only 1 line of code ,that's all...After all , dude ,both linq and ur code loops through the entire collection :) – Software Dev Apr 20 '18 at 13:13
  • 1
    With Linq you'd just replace a list of strings with a collection of byte arrays. The performance drain is in the calls to `SendMessage`. – juharr Apr 20 '18 at 13:13
  • I'm trying with linq and giving a try. – peter s Apr 20 '18 at 13:14
  • 1
    Possibly related? https://stackoverflow.com/questions/7545021/activemq-publish-messages-in-bulk-persistent-but-not-async – Dan Wilson Apr 20 '18 at 13:14
  • @Rajneesh when "*formatting mistakes*", be careful with indentation, you removed too much of it in the code block... – Rafalon Apr 20 '18 at 13:22
  • 2
    @peters You've asked for performance advice and ignored it from everyone. Simply searching 'foreach vs linq' will tell you how LINQ introduces overhead. `SendMessage()` is causing your drop in performance. – MalvEarp Apr 20 '18 at 13:28
  • 1
    @peters `This loop is killing the performance`. It's what's happening *inside* the loop which is killing performance. If you want to iterate over a collection, you want to iterate over a collection, no escaping that - but that `SendMessage` must be the suspect. –  Apr 20 '18 at 13:32

0 Answers0