:)  It does slow it down a little, but that's unavoidable.  It's also
unavoidable in C, Fortran, Perl, etc.  As long as the number of
operations in that loop is minimal, then it's the best you can do.  If
it worries you, you could run a test where you call the target
function say 1e6 times, with and without the loop to see the timing
difference.  Or simply running in Python 2:

for i in xrange(1000000):
 x = 1

Then try:

for i in xrange(100000000):
 x = 2

These two demonstrate the slowness of the Python loop.  But the second
case is extreme and you won't encounter that much looping in these
functions.  So while it is theoretically slower than C and Fortran
looping, you can probably see that no one would care :)  Here is
another test, with Python 2 code:

"""
import cProfile as profile

def loop_1e6():
    for i in xrange(int(1e6)):
        x = 1

def loop_1e8():
    for i in xrange(int(1e8)):
        x = 1

def sum_conv():
    for i in xrange(100000000):
        x = 2 + 2.

def sum_normal():
    for i in xrange(100000000):
        x = 2. + 2.

def test():
    loop_1e6()
    loop_1e8()
    sum_normal()
    sum_conv()

profile.runctx('test()', globals(), locals())
"""

Running this on my system shows:

"""
         7 function calls in 6.707 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    6.707    6.707 <string>:1(<module>)
        1    2.228    2.228    2.228    2.228 aaa.py:11(sum_conv)
        1    2.228    2.228    2.228    2.228 aaa.py:15(sum_normal)
        1    0.000    0.000    6.707    6.707 aaa.py:19(test)
        1    0.022    0.022    0.022    0.022 aaa.py:3(loop_1e6)
        1    2.228    2.228    2.228    2.228 aaa.py:7(loop_1e8)
        1    0.000    0.000    0.000    0.000 {method 'disable' of
'_lsprof.Profiler' objects}
"""

That should be self explanatory.  The better optimisation targets are
the repeated maths operations and the maths operations that can be
shifted into the target function or the target function
initialisation.  Despite the numbers above which prove my int to float
speed argument as utter nonsense, it might still good to remove the
int to float conversions, if only to match the other functions.

Regards,

Edward




On 5 May 2014 18:45, Troels Emtekær Linnet <tlin...@nmr-relax.com> wrote:
> The reason why I ask, is that I am afraid that this for loop slows
> everything down.
>
> What do you think?
>
> 2014-05-05 18:41 GMT+02:00 Edward d'Auvergne <edw...@nmr-relax.com>:
>> This is not Python specific though :)  As far as I know, C uses
>> pass-by-value for arguments, unless they are arrays or other funky
>> objects/functions/etc..  This is the same behaviour as Python.
>> Pass-by-reference and pass-by-value is something that needs to be
>> mastered in all languages, whether or not you have pointers to play
>> with.
>>
>> Regards,
>>
>> Edward
>>
>>
>>
>> On 5 May 2014 18:30, Troels Emtekær Linnet <tlin...@nmr-relax.com> wrote:
>>> This reminds me:
>>>
>>> http://combichem.blogspot.dk/2013/08/you-know-what-really-grinds-my-gears-in.html
>>>
>>> 2014-05-05 17:52 GMT+02:00 Edward d'Auvergne <edw...@nmr-relax.com>:
>>>> Hi,
>>>>
>>>> This is an important difference.  In the first case (back_calc[i] =
>>>> Minty[i]), what is happening is that your are copying the data into a
>>>> pre-existing structure.  In the second case (back_calc = Minty), the
>>>> existing back_calc structure will be overwritten.  Therefore the
>>>> back_calc structure in the calling code will be modified in the first
>>>> case but not the second.  Here is some demo code:
>>>>
>>>> def mod1(x):
>>>>     x[0] = 1
>>>>
>>>> def mod2(x):
>>>>     x = [3, 2]
>>>>
>>>> x = [0, 2]
>>>> print(x)
>>>> mod1(x)
>>>> print(x)
>>>> mod2(x)
>>>> print(x)
>>>>
>>>> I don't know of a way around this.
>>>>
>>>> Regards,
>>>>
>>>> Edward
>>>>
>>>>
>>>> On 5 May 2014 17:42, Troels Emtekær Linnet <tlin...@nmr-relax.com> wrote:
>>>>> Hi Edward.
>>>>>
>>>>> In the library function of b14.py, i am looping over
>>>>> the dispersion points to put in the data.
>>>>>
>>>>>     for i in range(num_points):
>>>>>
>>>>>         # The full formula.
>>>>>         back_calc[i] = Minty[i]
>>>>>
>>>>> Why can I not just set:
>>>>> back_calc = Minty
>>>>>
>>>>> _______________________________________________
>>>>> relax (http://www.nmr-relax.com)
>>>>>
>>>>> This is the relax-devel mailing list
>>>>> relax-devel@gna.org
>>>>>
>>>>> To unsubscribe from this list, get a password
>>>>> reminder, or change your subscription options,
>>>>> visit the list information page at
>>>>> https://mail.gna.org/listinfo/relax-devel

_______________________________________________
relax (http://www.nmr-relax.com)

This is the relax-devel mailing list
relax-devel@gna.org

To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-devel

Reply via email to