Hi Edward.

How about this script?
Here I try to pass the back the r2eff values, and then set them in the
back_calculated class object.
Will this work ??

Or else I found this post about updating values.
http://stackoverflow.com/questions/14916284/in-python-class-object-how-to-auto-update-attributes
They talk about
@property
and setter, which I dont get yet. :-)

Best
Troels


---------------


def loop_rep(x, nr):
    y = [98, 99]
    for i in range(nr):
        x[i] = y[i]

def not_loop_rep(x, nr):
    y = [98, 99]
    x = y

def not_loop_rep_new(x, nr):
    y = [98, 99]
    x = y
    return x


class MyClass:
    def __init__(self, x):
        self.x = x
        self.nr = len(x)

    def printc(self):
        print self.x, self.nr

    def calc_loop_rep(self, x=None, nr=None):
        loop_rep(x=self.x, nr=self.nr)

    def calc_not_loop_rep(self, x=None, nr=None):
        not_loop_rep(x=self.x, nr=self.nr)

    def calc_not_loop_rep_new(self, x=None, nr=None):
        self.x = not_loop_rep_new(x=self.x, nr=self.nr)

print("For class where we loop replace ")
"Create object of class"
t_rep = MyClass([0, 1])
"Print object of class"
t_rep.printc()
"Calc object of class"
t_rep.calc_loop_rep()
" Then print"
t_rep.printc()

print("\nFor class where we not loop replace ")
" Now try with replace "
t = MyClass([3, 4])
t.printc()
t.calc_not_loop_rep()
t.printc()

print("\nFor class where we not loop replace ")
t_new = MyClass([5, 6])
t_new.printc()
t_new.calc_not_loop_rep_new()
t_new.printc()

2014-05-05 19:07 GMT+02:00 Edward d'Auvergne <[email protected]>:
> :)  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 <[email protected]> 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 <[email protected]>:
>>> 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 <[email protected]> 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 <[email protected]>:
>>>>> 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 <[email protected]> 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
>>>>>> [email protected]
>>>>>>
>>>>>> 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
[email protected]

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