score:5
as far as i understand the changes.
the idea is that for collections that do not have a o(1) (constant) size
. then, sizeis
can be more efficient, specially for comparisons with small values (like 1 in the comment).
but why?
simple, because instead of computing all the size and then doing the comparison, sizeis
returns an object which when computing the comparison, can return early.
for example, lets check the code
def sizecompare(othersize: int): int = {
if (othersize < 0) 1
else {
val known = knownsize
if (known >= 0) integer.compare(known, othersize)
else {
var i = 0
val it = iterator
while (it.hasnext) {
if (i == othersize) return if (it.hasnext) 1 else 0 // here!!! - return as fast as possible.
it.next()
i += 1
}
i - othersize
}
}
}
thus, in the example of the comment, suppose a very very very long list of three elements. sizeis > 1
will return as soon as it knows that the list has at least one element and hasmore
. thus, saving the cost of traversing the other two elements to compute a size of 3 and then doing the comparison.
note that: if the size of the collection is greater than the comparing value, then the performance would be roughly the same (maybe slower than just size
due the extra comparisons on each cycle). thus, i would only recommend this for comparisons with small values, or when you believe the values will be smaller than the collection.
Source: stackoverflow.com
Related Query
- Difference between size and sizeIs
- Scala - What is the difference between size and length of a Seq?
- Difference between object and class in Scala
- What is the difference between Scala's case class and class?
- What is the difference between self-types and trait subclasses?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- Difference between a Seq and a List in Scala
- What is the difference between a var and val definition in Scala?
- Difference between method and function in Scala
- Difference between case object and object
- What is the difference between "def" and "val" to define a function
- difference between foldLeft and reduceLeft in Scala
- What is the difference between JavaConverters and JavaConversions in Scala?
- What's the difference between == and .equals in Scala?
- Difference between Array and List in scala
- What's the difference between => , ()=>, and Unit=>
- Difference between this and self in self-type annotations?
- What’s the difference between ScalaTest and Scala Specs unit test frameworks?
- What is the difference between build.sbt and build.scala?
- Scala: What is the difference between Traversable and Iterable traits in Scala collections?
- Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?
- Difference between Abstract Class and Trait
- Difference between using App trait and main method in scala
- What's the difference between :: and ::: in Scala
- What's the difference between multiple parameters lists and multiple parameters per list in Scala?
- What's the difference between A<:B and +B in Scala?
- What's the difference between Unit and Nothing?
- Difference between fold and foldLeft or foldRight?
- Difference between case class and case object?
- Difference between forward and tell in akka actors
More Query from same tag
- Is is reasonable, and is there a benefit to a Scala Symbol class that extends AnyVal?
- Spark Scala: Querying same table multiple times
- Type mismatch error in Scala
- Declare variable to use inside @helper.repeat - PlayFramework
- Translation of Scala code section for a map method/function
- Scala - extract Either type parameters
- Protected functions in Scala
- Type safety when optional field is guaranteed to be present
- Get JsonSubType array contents in Scalatra project
- Upickle: read an attribute that may be a String or Int as a String
- Applying evolutions to ScalikeJDBC in-memory test DB
- What's the difference of the Akka's Actor with Scala's Actor model
- Dataset forEach loop in scala throwing SparkException Task not serializable
- How do I set a log4j2 configuration file for heroku in my scala play application?
- How can I add extra behaviour only to Lists of a certain type?
- Testing Akka Typed behavior
- Spark: optimise writing a DataFrame to SQL Server
- Add a new line in front of each line before writing to JSON format using Spark in Scala
- Need help figuring out scala compiler errors
- scala: How to handle Option in a functional way
- accessing host ip and port in play framework scala template?
- Sending a message to all actors within an ActorSystem
- I'm applying the same methods to multiple dataframes in spark scala, how can I parallelize this?
- How to log Filename and Linenumber in Akka Classic Actor?
- Deduping evnts using hiveContext in spark with scala
- How to manually expire Play 2 cache
- How to drop first row from parquet file?
- Cannot import class router.Routes in ApplicationLoader on Play 2.5
- Spark Scala DateType schema execution error
- Replace seperator in Array[long] in the Spark dataframe