1/16/2015

range() function in C++ : Inspired from Python

# What is range() function, and how it can help you ?
    # A sequence of numbers from starting point to end point ( End point may be undefined, so you have a Infinite range), and optional step parameter.
    # You can generate a sequence integers ,
        example:-
            range(1, 10)  # Exclusive ranges == [start, end, step)
            [1, 2, 3, 4, 5, 6, 7, 8, 9]

            range(1, 10)  # Inclusive ranges == [start, end, step]
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # You are not limited to generate ranges of integers, you can also generate ranges from  char / float ( * )  data type.
        example:-
            range('a', 'z', 1)    # Exclusive ranges == [start, end, step)
            a b c d e f g h i j k l m n o p q r s t u v w x y

            range('A', 'z', 1 )    # Inclusive ranges = [start, end, step]
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z


    # More Examples :- ( Exclusive ranges )
        std::vector<char> v = rangeE('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y

        std::vector<char> v = rangeE('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w

        std::vector<int> v = rangeE(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96

        std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 343.497 343.897 344.297 344.697 345.097

        std::vector<int> v = rangeE(10, 100);
            10 11 12 13 ,... 97 98 99

        std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 97 97.5 98 98.5 99 99.5

        std::vector<char> v = rangeE('A', 'z', 1  );
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y

    # More Examples :- ( Inclusive ranges )
        std::vector<char> v = rangeI('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y z

        std::vector<char> v = rangeI('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w z

        std::vector<int> v = rangeI(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100

        std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 345.097 345.497 345.897 346.297


        std::vector<int> v = rangeI(10, 100);
            10 11 12 13 ,.. 97 98 99 100

        std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 98 98.5 99 99.5 100 100.5

## range() function using C++
### Exclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeE(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeE('a', 'z', 1  );
    // std::vector<char> v = rangeE('A', 'z', 3);
    // std::vector<int> v = rangeE(0, 100, 4);
    // std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeE(10, 100);
    std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}

### Inclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeI(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop+1) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeI('a', 'z', 1  );
    // std::vector<char> v = rangeI('A', 'z', 3);
    // std::vector<int> v = rangeI(0, 100, 4);
    // std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeI(10, 100);
    std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}



# Tests :-

## Does our range() function can be directly used in other programs?
## What about its performance?
## Does it comparable to native range() function in C++?

### Implementing and using native range() function in C++11
iteration = 10000000; Language = C++11
# Write output to Console
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRangeDef
10.5701301098
10.5873568058
10.4189360142
10.7807810307
10.10414505

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
0.556797981262
1.02536702156
1.04401111603
0.961767911911
0.982132911682
0.971391916275
0.934329032898
1.09931206703
0.961050033569
0.937591075897


iteration = 10000000*10; Language = C++11
# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
6.75697207451
6.55503702164
8.64361310005
5.96445202827
7.79801106453
6.90669894218
6.0758459568
6.74977111816
7.17069101334
7.01367592812



### Implementing and using range() Function with another function in C++11

iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRange
10.5833230019
10.6844108105
10.6519501209
10.6691529751
10.6650300026

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRange > dumploop.txt"
0.753065824509
1.19918298721
1.18582081795
1.1427628994
1.60647511482
1.0098259449
1.18290019035
1.12054014206
1.10526990891
1.14365291595

iteration = 10000000; Language = C++11; Implementation = using C++ Template
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTemp > dumploop.txt"
1.16944003105
0.979794979095
1.31697702408
0.782207012177
1.13583612442
1.56683707237
1.01916909218
1.18384790421
0.986218214035
1.383425951

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000*10; Language = C++11
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRange > dumploop.txt"
8.63241100311
8.56675696373
6.39435505867
8.33173489571
8.47799301147
8.60728192329
8.34282588959
7.66121196747
7.68087077141
8.34091496468


# Testing our built function within nested loop
# iteration = 1000x1000; Language = C++11
# Code :-

    for(auto i: rangeE(0, 100, 2)) {
        for(auto j: rangeE(0, 100, 2)) {
            cout << i << ' ' << j << endl;
        }
    }

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTempNest0 > dumploop.txt"
1.52887296677
1.60038113594
1.56652903557
1.78646588326
1.50389409065
1.59423518181
1.56246495247
1.57362604141
1.58079385757
1.56350493431

# iteration = 1000x1000; Language = C++11
# Code :-

    for(int i=0; i< 1000; i+=2) {
        for(int j=0; j< 1000; j+=2) {
            cout << i << ' ' << j << endl;
        }
    }
    cout << endl;

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDefNest0 > dumploop.txt"
1.5027050972
1.65471196175
1.49789905548
1.7509188652
1.52278089523
1.58404016495
1.52930808067
1.54360198975
1.54617094994
1.54985117912


# Built-in Python  range() function tests :-

iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveListPY.py
11.4798099995
11.4038779736
11.5232269764
11.460515976
11.6050000191

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveListPY.py > dumploop.txt"
2.33868694305
2.60927009583
2.50296115875
2.53171110153
2.93597483635
2.12743091583
2.47753596306
2.4461350441
2.69060111046
2.41603589058

# NOTE :- testing of programs is done with Python Script, that will tell you execution time taken by a program.
# PC conf :-
Intel CORE 2 DUO ( 2GB RAM )
LinuxMint 17 - x64


Post a Comment