7

What is the best practice when using collections.

Example:

This snippet:

shortStudentMap = new Map<string,string>();
shortStudentMap.put('1', 'Student1');
shortStudentMap.put('2', 'Student2');
shortStudentMap.put('3', 'Student3');
shortStudentMap.put('4', 'Student4');
shortStudentMap.put('5', 'Student5');
shortStudentMap.put('6', 'Student6');

Or this snippet:

shortStudentMap = new Map<string,string>
{
     1 => 'Student1',
     2 => 'Student2',
     3 => 'Student3',
     4 => 'Student4',
     5 => 'Student5',
     6 => 'Student6'
};
  • 1
    The second one will be translated to the first format in the compiling time. The second one is good for typing, the first one is good for compiler – sotondolphin Sep 10 '14 at 9:00
  • 1
    Would you mind updating the title so something more specific than "using" ? For instance "Defining and populating collections" may cover the content of your question better, making it more discoverable for otheres with the same question. – Samuel De Rycke Sep 10 '14 at 13:12
  • 1
    See the answer starting "The time reporting in the original code appears broken"; the performance difference between the two patterns is insignificant. – Keith C Sep 10 '14 at 15:19
  • @SamuelDeRycke better? – rpm07 Sep 10 '14 at 20:32
  • 1
    Definitely, thanks. I like questions like these, so i figured a more specific title would be good. – Samuel De Rycke Sep 11 '14 at 8:09
8

The time reporting in the original code appears broken. (The code in the earlier answer.)

Running the code below shows that there isn't a significant difference in performance, both reporting 18 to 22 ms with the "=>" approach normally being faster than the "put" approach.

@isTest
Public Class speed{

    public void try1(){
        Map<string,string> shortStudentMap;
        shortStudentMap = new Map<string,string>();
        shortStudentMap.put('1', 'Student1');
        shortStudentMap.put('2', 'Student2');
        shortStudentMap.put('3', 'Student3');
        shortStudentMap.put('4', 'Student4');
        shortStudentMap.put('5', 'Student5');
        shortStudentMap.put('6', 'Student6');
    }

    public void try2(){
        Map<string,string> shortStudentMap = new Map<string,string> {
             '1' => 'Student1',
             '2' => 'Student2',
             '3' => 'Student3',
             '4' => 'Student4',
             '5' => 'Student5',
             '6' => 'Student6'
        };   
    }

    @isTest
    static void test() {

        Speed s = new Speed();

        long t1 = System.currentTimeMillis();
        for(Integer i =0; i < 100; i++){
            s.try1();
        }
        long t2 = System.currentTimeMillis();

        long t3 = System.currentTimeMillis();
        for(Integer i =0; i < 100; i++){
            s.try2();
        }
        long t4 = System.currentTimeMillis();

        System.debug('ms1=' + (t2 - t1));
        System.debug('ms2=' + (t4 - t3));
    }
}
| improve this answer | |
  • 1
    Hooray. That not only makes more sense, but means my preferred method is still the "best" if only very marginally. – Dominic Sep 10 '14 at 16:06
  • 1
    Thank you for the correction Keith. I agree there is not much difference. – AtulRajguru9 Sep 11 '14 at 4:43
  • @KeithC have not noticed the datetime and currentTimeMillis, thank you for the correction – rpm07 Sep 11 '14 at 5:39
6

Bellow is not correct answer. For calculating time lapsed we should use System.currentTimeMillis() and not datetime.now() . For correct answer please refer Keith C post

Here is how:

Created a following class:

Public Class speed{

public void try1(){
    Map<string,string> shortStudentMap;
    shortStudentMap = new Map<string,string>();
    shortStudentMap.put('1', 'Student1');
    shortStudentMap.put('2', 'Student2');
    shortStudentMap.put('3', 'Student3');
    shortStudentMap.put('4', 'Student4');
    shortStudentMap.put('5', 'Student5');
    shortStudentMap.put('6', 'Student6');
}

public void try2(){
Map<string,string> shortStudentMap;

shortStudentMap = new Map<string,string>
    {
         '1' => 'Student1',
         '2' => 'Student2',
         '3' => 'Student3',
         '4' => 'Student4',
         '5' => 'Student5',
         '6' => 'Student6'
    };
}     
}

And then run following code in developer console:

Speed s = new Speed();

datetime start1 = datetime.now();
for(Integer i =0; i < 100; i++){
    s.try1();
}
datetime end1 = datetime.now();
System.debug('Time Laps Try 1:' + (end1.getTime() - start1.gettime()));


datetime start2 = datetime.now();
for(Integer i =0; i < 100; i++){
    s.try2();
}
datetime end2 = datetime.now();
System.debug('Time Laps Try 2:' + (end2.getTime() - start2.gettime()));

I run above test many times and found that Try1 runs faster than try 2 every time.

So i would recommend you to use put method as best practice instead of providing large string to Slesforce . This will improve performance & readability of your code.

| improve this answer | |
  • this great, I did not know about that. – rpm07 Sep 10 '14 at 5:58
  • you were right. 1. try 1 - 1:22 2. try 2 - 2:23 – rpm07 Sep 10 '14 at 6:46
  • 2
    There used to be a strong reason for preferring the second way. When we were limited to 200k lines of code we could save some lines by initialising a map or list in a single line, but that is no longer the case. I still prefer the single-line method for neatness, but maybe I'll change now based on the performance difference you've found. – Dominic Sep 10 '14 at 8:37
  • See my answer that starts with "The time reporting in the original code appears broken". – Keith C Sep 10 '14 at 12:54
  • 1
    Now I have a ton of code to re-write... how naive of us developers to assume that the constructor would be faster. – sfdcfox Sep 10 '14 at 20:41
2

For Initializing the static data the following Snippet is useful but not for dynamic data..

shortStudentMap = new Map<string,string>
{
     1 => 'Student1',
     2 => 'Student2',
     3 => 'Student3',
     4 => 'Student4',
     5 => 'Student5',
     6 => 'Student6'
};

So Its best practice to use the following snippet to initialize with static data or add dynamic data.

shortStudentMap = new Map<string,string>();
shortStudentMap.put('1', 'Student1');
shortStudentMap.put('2', 'Student2');
shortStudentMap.put('3', 'Student3');
shortStudentMap.put('4', 'Student4');
shortStudentMap.put('5', 'Student5');
shortStudentMap.put('6', 'Student6');
| improve this answer | |
  • I think if have a static data is better to use the first snippet, but if I want dynamic data I should use the second snippet. It is about whether your data is static or dynamic. – rpm07 Sep 10 '14 at 5:55
  • 1
    And also Compare to first snippet second snippet is better because Its easy and Simple to add/modify records.Performance wise also second snippet is fast.. so Its better to use Put method always.. – Murali Mohan Mandadi Sep 10 '14 at 7:21
2

Surprising that there is a significant difference in performance.

But bear in mind that there are other factors than performance when judging "best" and the ability to initialize a map without having to create a variable that pollutes the scope and clutters the code is a good language feature that is missing from e.g. Java that is worth using sometimes e.g.:

     ...
     someMethod(new Map<string,string>{
             '1' => 'Student1',
             '2' => 'Student2',
             '3' => 'Student3'
              });
     someMethod(new Map<string,string>{
             '4' => 'Student4',
             '5' => 'Student5',
             '6' => 'Student6'
              });
     someMethod(new Map<string,string>{
             '7' => 'Student7',
             '8' => 'Student8',
             '9' => 'Student9'
              });
     ...

 public void someMethod(Map<Integer, String> m) {
     ...
 }
| improve this answer | |
  • it is a matter of situation, if performance is highly required then I should use the first snippet I included. – rpm07 Sep 10 '14 at 9:11
  • @robertpeter07 Agreed. Worth keeping this in mind too c2.com/cgi/wiki?PrematureOptimization. – Keith C Sep 10 '14 at 9:21
  • will keep this in mind – rpm07 Sep 10 '14 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.