GitHub user paplorinc opened a pull request:

    https://github.com/apache/incubator-groovy/pull/120

    Changed the Range's step to Number instead of int

    Until now a range could only step by an integer value, even if its elements 
had other types, e.g. doubles.
    This made stepping over ranges like `0.1..0.9`strange, as that resulted in 
a single element.
    
    Also, stepping over `ObjectRange` instances (like the above one) called the 
`next`/`previous` method reflectively,
    even if the range had a numeric type (the most common case).
    This made it ~10x slower than e.g. `IntRange`.
    
    The new implementation changed the `step` type to `Number` and unified the 
way stepping is done in
    `ObjectRange` and `IntRange`, making them equally fast (close to the 
original `IntRange` speed)
    for numeric cases.
    
    Also, `IntRange` treats the overflow issue by storing the intermediary 
value in a `long` directly.
    
    
    -------------------
    
    I tested the speed via the following trivial method:
    
    ```Groovy
    void testSpeed() {
        for (def i = 0; i < 10; i++)
            run()
    }
    
    private void run() {
        def time = System.currentTimeMillis()
        def size = 0G
        for (def i = 0; i < 100; i++) { // changed to `i = 0G` when testing 
ObjectRange
            for (def j = 0; j < 100; j++) {
                def range = i..j
                def inverseRange = j..i
                for (def k = 1; k < 100; k++) {
                    size += range.step(k).size()
                    size += range.step(-k).size()
    
                    size += inverseRange.step(k).size()
                    size += inverseRange.step(-k).size()
                }
            }
        }
        println "size = ${size} in ${(System.currentTimeMillis() - time) / 
1000.0}s"
    }
    ```
    
    resulting in
    ```
    * Old impl for ObjectRange: `66.30s`
    * New impl for ObjectRange: `5.52s` (12 times faster)
    ```
    and
    ```
    * Old impl for IntRange: `4.54s`
    * New impl for IntRange: `4.97s` (0.91 times faster)
    ```

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/paplorinc/incubator-groovy RangeNumberStepSize

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-groovy/pull/120.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #120
    
----
commit 905ccfc0cfa848eea45e3d33f3dc542a40512d2a
Author: Pap Lőrinc <paplor...@yahoo.com>
Date:   2015-09-17T19:45:24Z

    Changed the Range's step to Number instead of int
    
    Until now a range could only step by an integer value, even if its elements 
had other types, e.g. doubles.
    This made stepping over ranges like `0.1..0.9`strange, as that resulted in 
a single element.
    
    Also, stepping over `ObjectRange` instances (like the above one) called the 
`next`/`previous` method reflectively,
    even if the range had a numeric type (the most common case).
    This made it ~10x slower than e.g. `IntRange`.
    
    The new implementation changed the `step` type to `Number` and unified the 
way stepping is done in
    `ObjectRange` and `IntRange`, making them equally fast (close to the 
original `IntRange` speed)
    for numeric cases.
    
    Also, `IntRange` treats the overflow issue by storing the intermediary 
value in a `long` directly.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to