# Assoc library in c (or C++)

**URL:** <https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338>\
**Category:** General\
**Created:** [March 1, 2019, 11:48am UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338 "2019-03-01T11:48:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 1, 2019, 11:48am UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/1 "2019-03-01T11:48:31Z")

</div>

Hello,

I started to examine my program for performance. Since i am making heavy use of assoc (and its relational “reading” ) i am wondering if there could be a significant performance advantage if the assoc library would be implemented in C (or C++?).

Any thoughts would be much appreciated,

thank you,

Dan

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 1, 2019, 1:45pm UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/2 "2019-03-01T13:45:07Z")

</div>

If the time spent is mostly lookup, the performance difference will not be big.

You need to understand the use case and profile to see where the time goes. If you often insert and remove items from the assoc, it will have to re-balance and this is written in Prolog.

On the other hand, if you are not inserting and removing often, you might even try to use dicts and see if that makes a difference.

So if you can see what is currently the bottle neck and understand your use case, it will be easier to make a decision before investing time into writing more code.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 1, 2019, 2:15pm UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/3 "2019-03-01T14:15:39Z")

</div>

Hi Boris,

Thank you for your explanation – thats very helpful.

Indeed, i am heavily using it for read and write operations all the time. Less for removal though, since usually that happens due to backtracking.

So, rebalancing is an expensive operation that could see a speed up with c or c++?

Dan

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 1, 2019, 2:46pm UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/4 "2019-03-01T14:46:21Z")

</div>

Do you have any idea how much of the total time of your test case goes into adding items to the assoc? Or for lookup? Once you know that you can at least put a hard upper limit to the possible benefits of optimizing this particular part of your program.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 1, 2019, 2:55pm UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/5 "2019-03-01T14:55:27Z")

</div>

I hope i am reading the profile data correctly.

For one use case when i order “cumulative profile by time”, then “addkey” is about 17% and “lookup” around 16%

the rest of the time is sort of evenly distributedl no particular “hotspot” as far as i can see

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 1, 2019, 2:57pm UTC](https://swi-prolog.discourse.group/t/assoc-library-in-c-or-c/338/6 "2019-03-01T14:57:33Z")

</div>

thinking about it – assoc is involved more than 30% of the computational time … that seems quite significant to me - i guess this is an area i should think about very carefully.
