Which is more efficient - Function call multiple times or setting variable?












0















In Swift 4 which is a more efficient way: Making a function call once and storing the return in a variable (or let) then using that variable multiple times OR making the function call multiple times?



For example using this function (or something equally simple):



func myMeth()->String{
return "this string"
}


Then:



let myVar = myMeth()
var1 = myVar
var2 = myVar
var3 = myVar
var4 = myVar


OR:



var1 = myMeth()
var2 = myMeth()
var3 = myMeth()
var4 = myMeth()









share|improve this question

























  • In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

    – matt
    Nov 25 '18 at 4:05











  • @matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

    – wayneh
    Nov 25 '18 at 4:15






  • 1





    Only real way to know: measure. That’s what Instruments is for.

    – matt
    Nov 25 '18 at 4:17











  • @matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

    – wayneh
    Nov 25 '18 at 4:19











  • Why not use the get and set blocks instead?

    – Rakesha Shastri
    Nov 25 '18 at 4:30
















0















In Swift 4 which is a more efficient way: Making a function call once and storing the return in a variable (or let) then using that variable multiple times OR making the function call multiple times?



For example using this function (or something equally simple):



func myMeth()->String{
return "this string"
}


Then:



let myVar = myMeth()
var1 = myVar
var2 = myVar
var3 = myVar
var4 = myVar


OR:



var1 = myMeth()
var2 = myMeth()
var3 = myMeth()
var4 = myMeth()









share|improve this question

























  • In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

    – matt
    Nov 25 '18 at 4:05











  • @matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

    – wayneh
    Nov 25 '18 at 4:15






  • 1





    Only real way to know: measure. That’s what Instruments is for.

    – matt
    Nov 25 '18 at 4:17











  • @matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

    – wayneh
    Nov 25 '18 at 4:19











  • Why not use the get and set blocks instead?

    – Rakesha Shastri
    Nov 25 '18 at 4:30














0












0








0








In Swift 4 which is a more efficient way: Making a function call once and storing the return in a variable (or let) then using that variable multiple times OR making the function call multiple times?



For example using this function (or something equally simple):



func myMeth()->String{
return "this string"
}


Then:



let myVar = myMeth()
var1 = myVar
var2 = myVar
var3 = myVar
var4 = myVar


OR:



var1 = myMeth()
var2 = myMeth()
var3 = myMeth()
var4 = myMeth()









share|improve this question
















In Swift 4 which is a more efficient way: Making a function call once and storing the return in a variable (or let) then using that variable multiple times OR making the function call multiple times?



For example using this function (or something equally simple):



func myMeth()->String{
return "this string"
}


Then:



let myVar = myMeth()
var1 = myVar
var2 = myVar
var3 = myVar
var4 = myVar


OR:



var1 = myMeth()
var2 = myMeth()
var3 = myMeth()
var4 = myMeth()






swift4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 5:10









rmaddy

240k27315379




240k27315379










asked Nov 25 '18 at 3:59









waynehwayneh

1,94872348




1,94872348













  • In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

    – matt
    Nov 25 '18 at 4:05











  • @matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

    – wayneh
    Nov 25 '18 at 4:15






  • 1





    Only real way to know: measure. That’s what Instruments is for.

    – matt
    Nov 25 '18 at 4:17











  • @matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

    – wayneh
    Nov 25 '18 at 4:19











  • Why not use the get and set blocks instead?

    – Rakesha Shastri
    Nov 25 '18 at 4:30



















  • In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

    – matt
    Nov 25 '18 at 4:05











  • @matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

    – wayneh
    Nov 25 '18 at 4:15






  • 1





    Only real way to know: measure. That’s what Instruments is for.

    – matt
    Nov 25 '18 at 4:17











  • @matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

    – wayneh
    Nov 25 '18 at 4:19











  • Why not use the get and set blocks instead?

    – Rakesha Shastri
    Nov 25 '18 at 4:30

















In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

– matt
Nov 25 '18 at 4:05





In general in any language a function call will have overhead that merely copying a value will not, though a good compiler may help to make up for it somewhat.

– matt
Nov 25 '18 at 4:05













@matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

– wayneh
Nov 25 '18 at 4:15





@matt So assuming my actual method is as simple as my example (like a set/get property) it shouldn't matter unless the function calls are extremely numerous - like hundreds or thousands as opposed to 10's ?

– wayneh
Nov 25 '18 at 4:15




1




1





Only real way to know: measure. That’s what Instruments is for.

– matt
Nov 25 '18 at 4:17





Only real way to know: measure. That’s what Instruments is for.

– matt
Nov 25 '18 at 4:17













@matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

– wayneh
Nov 25 '18 at 4:19





@matt True....I think the measured values I'm dealing with are so small it may not matter and may just be a coding style preference.

– wayneh
Nov 25 '18 at 4:19













Why not use the get and set blocks instead?

– Rakesha Shastri
Nov 25 '18 at 4:30





Why not use the get and set blocks instead?

– Rakesha Shastri
Nov 25 '18 at 4:30












2 Answers
2






active

oldest

votes


















0














Usually I would say store the return value in a variable but that depends on whether what the function will return will change later on. For your example use a variable as running a function will have more overhead especially if it is a complicated one.






share|improve this answer
























  • My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

    – wayneh
    Nov 25 '18 at 4:18











  • Every time that the result will have changed re-execute the function to update the variable or create a new variable.

    – Tyler
    Nov 26 '18 at 4:00



















0














Calling a function and store result in a variable both are different things. It depends what you need.




  1. Calling a function is better when you don't want to store a value in a variable. You just want to access the value wherever you need.


  2. Store in a variable, use it when you want to persist the result globally within that viewController.



If you store the result in a variable, it will always consume memory until your ViewController is in memory but calling a function will take processing memory only when you will call it.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464532%2fwhich-is-more-efficient-function-call-multiple-times-or-setting-variable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Usually I would say store the return value in a variable but that depends on whether what the function will return will change later on. For your example use a variable as running a function will have more overhead especially if it is a complicated one.






    share|improve this answer
























    • My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

      – wayneh
      Nov 25 '18 at 4:18











    • Every time that the result will have changed re-execute the function to update the variable or create a new variable.

      – Tyler
      Nov 26 '18 at 4:00
















    0














    Usually I would say store the return value in a variable but that depends on whether what the function will return will change later on. For your example use a variable as running a function will have more overhead especially if it is a complicated one.






    share|improve this answer
























    • My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

      – wayneh
      Nov 25 '18 at 4:18











    • Every time that the result will have changed re-execute the function to update the variable or create a new variable.

      – Tyler
      Nov 26 '18 at 4:00














    0












    0








    0







    Usually I would say store the return value in a variable but that depends on whether what the function will return will change later on. For your example use a variable as running a function will have more overhead especially if it is a complicated one.






    share|improve this answer













    Usually I would say store the return value in a variable but that depends on whether what the function will return will change later on. For your example use a variable as running a function will have more overhead especially if it is a complicated one.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 4:09









    TylerTyler

    11




    11













    • My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

      – wayneh
      Nov 25 '18 at 4:18











    • Every time that the result will have changed re-execute the function to update the variable or create a new variable.

      – Tyler
      Nov 26 '18 at 4:00



















    • My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

      – wayneh
      Nov 25 '18 at 4:18











    • Every time that the result will have changed re-execute the function to update the variable or create a new variable.

      – Tyler
      Nov 26 '18 at 4:00

















    My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

    – wayneh
    Nov 25 '18 at 4:18





    My method is used like a set/get property, so typically the retrieved value does not change during the time a View is displayed, but may change before the next time the View is shown. So I could 'get' it when the viewwillappear for example.

    – wayneh
    Nov 25 '18 at 4:18













    Every time that the result will have changed re-execute the function to update the variable or create a new variable.

    – Tyler
    Nov 26 '18 at 4:00





    Every time that the result will have changed re-execute the function to update the variable or create a new variable.

    – Tyler
    Nov 26 '18 at 4:00













    0














    Calling a function and store result in a variable both are different things. It depends what you need.




    1. Calling a function is better when you don't want to store a value in a variable. You just want to access the value wherever you need.


    2. Store in a variable, use it when you want to persist the result globally within that viewController.



    If you store the result in a variable, it will always consume memory until your ViewController is in memory but calling a function will take processing memory only when you will call it.






    share|improve this answer




























      0














      Calling a function and store result in a variable both are different things. It depends what you need.




      1. Calling a function is better when you don't want to store a value in a variable. You just want to access the value wherever you need.


      2. Store in a variable, use it when you want to persist the result globally within that viewController.



      If you store the result in a variable, it will always consume memory until your ViewController is in memory but calling a function will take processing memory only when you will call it.






      share|improve this answer


























        0












        0








        0







        Calling a function and store result in a variable both are different things. It depends what you need.




        1. Calling a function is better when you don't want to store a value in a variable. You just want to access the value wherever you need.


        2. Store in a variable, use it when you want to persist the result globally within that viewController.



        If you store the result in a variable, it will always consume memory until your ViewController is in memory but calling a function will take processing memory only when you will call it.






        share|improve this answer













        Calling a function and store result in a variable both are different things. It depends what you need.




        1. Calling a function is better when you don't want to store a value in a variable. You just want to access the value wherever you need.


        2. Store in a variable, use it when you want to persist the result globally within that viewController.



        If you store the result in a variable, it will always consume memory until your ViewController is in memory but calling a function will take processing memory only when you will call it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 5:27









        iDev750iDev750

        5931315




        5931315






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464532%2fwhich-is-more-efficient-function-call-multiple-times-or-setting-variable%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)