Are methods class instances that consume a lot of memory (in Scala)?

Situation

I am going to create a program (in Scala or Python - not yet accepted) that works intensively with data. I see two approaches of the mayor:

  • Approach: Define a data collection. Write my function. Send the entire data set through the function.
  • Approach: Define a data class that represents a single data object and encodes a method (class member) into a data class. Parts of the method that must be flexible are sent to the method using the Scala Function or Python lambda.

Side question

I'm not sure, but the first approach may be more functional, for example, the second OOP, right? By the way, I like both functional programming and OOP (some say that they are opposite to each other, but Odersky tried his best to refute this with Scala).

Main question

I prefer the second approach because

  • It seems more concise to me.
  • This simplifies the distribution of the program in Big Data setup without general settings, since it brings functionality to the data, not data to the functionality, following the principle of data locality.

However, I am worried that if I have a lot of data (and I will), I will have a lot of memory consumption, because the method may have to be created so many times.

  • Question: This is true for Scala / JVM - if not, how to solve it?
  • : Python - , ?

: ?

  • (, ).
  • . , , 10.
  • , .
  • , 100 , 100 * 1 .
  • , .
  • , DataObject , . , JVM Python , - .

DataObject:

class DataObject {

    List datavalues

    def mymethod(){
        ...
    }
}
+4
1

. , . , / , .

. , , , , . , , . , , .

, , , . . / , . - , x, , , , a, b d . - f, , - k, -. -foo, , .

, , . , .


, python. .

python . , , , , . , - , . , , , .

, - , , self . .

# Method Call
$ python -m timeit -s 'class Foo():' -s ' def p(self):' -s '  pass' -s 'foo = Foo()' 'foo.p()'
10000000 loops, best of 3: 0.158 usec per loop
# Method Call of cached method
$ python -m timeit -s 'class Foo():' -s ' def p(self):' -s '  pass' -s 'foo = Foo()' -s 'p=foo.p' 'p()'
10000000 loops, best of 3: 0.0984 usec per loop
# Function Call
$ python -m timeit -s 'def p():' -s ' pass' 'p()'
10000000 loops, best of 3: 0.0846 usec per loop

; , , , .

, , /. , .

, , . , .

+1

Source: https://habr.com/ru/post/1655441/


All Articles