Steven Roose
2015-06-02 15:14:18 UTC
Let's say I'd want a method that wants to return a new object instance from
a class depending on the input parameter (Strings f.e.). Consider a
situation in which there are a large number of maps required and only some
of them will be used (although very frequently).
There are several approaches for this:
https://gist.github.com/stevenroose/dc057850a9dbf8323bda
Now, the first is just if-else and is just to state the problem. It's not a
real option.
Then, there is one switch-based and two map-based approaches.
- HashMaps in Dart work constant time, that's great. However, the map
stays in memory as long as the Mapper class exists. I already mentioned
there will be a lot of these maps required, so that might not be what we
want.
Hence, time O(1), memory O(N)
- Switch on the other hand does not save anything in memory. However,
they work in linear time, which can be a lot slower when a large number of
items is added.
Hence, time O(N), memory O(1)
So how to evaluate these approaches? Is there a best-practice? At what
point is memory usage accepted to improve performance?
Better specifying the problem, it might be relevant to add that there
doesn't necessarily have to be a single mapping function, but the maps can
be divided into categories. F.e. mapHeadgear and mapFootwear. In this case,
the amount of memory needed is still O(N) for the map approaches, but the
time complexity for the switches changes to O(N/C) (with C the number of
categories).
Any thoughts?
Thanks!
a class depending on the input parameter (Strings f.e.). Consider a
situation in which there are a large number of maps required and only some
of them will be used (although very frequently).
There are several approaches for this:
https://gist.github.com/stevenroose/dc057850a9dbf8323bda
Now, the first is just if-else and is just to state the problem. It's not a
real option.
Then, there is one switch-based and two map-based approaches.
- HashMaps in Dart work constant time, that's great. However, the map
stays in memory as long as the Mapper class exists. I already mentioned
there will be a lot of these maps required, so that might not be what we
want.
Hence, time O(1), memory O(N)
- Switch on the other hand does not save anything in memory. However,
they work in linear time, which can be a lot slower when a large number of
items is added.
Hence, time O(N), memory O(1)
So how to evaluate these approaches? Is there a best-practice? At what
point is memory usage accepted to improve performance?
Better specifying the problem, it might be relevant to add that there
doesn't necessarily have to be a single mapping function, but the maps can
be divided into categories. F.e. mapHeadgear and mapFootwear. In this case,
the amount of memory needed is still O(N) for the map approaches, but the
time complexity for the switches changes to O(N/C) (with C the number of
categories).
Any thoughts?
Thanks!
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.