0

So I used the same splaytree used in geeksforgeeks and did some experiment with it:

code: https://www.geeksforgeeks.org/insertion-in-splay-tree/?ref=rp

vers.1

    #include <ctime>
    // the code for splaytree is too long so I just put the link here: https://www.geeksforgeeks.org/insertion-in-splay-tree/?ref=rp
    double t1=clock();
    for(int i = 0; i < 1000000; i++)
    {
        x = rand()%max_x;
        root = insert(root, x);
    }

    double t2=clock();
    double result = (t2-t1)/CLOCKS_PER_SEC;
    printf("insertion time: %f\n", result);

vers.2

    int x;
    double t1=clock();
    for(int i = 0; i < 1000000; i++)
    {
        x = rand()%max_x;
        root = insert(root, i);
    }

    double t2=clock();
    double result = (t2-t1)/CLOCKS_PER_SEC;
    printf("insertion time: %f\n", result);

OUTPUT:

for vers. 1: the insertion time is cpp insertion time: 0.779000 for vers. 2: the insertion time is insertion time: 0.156000

I know Splay tree "splays" every time it does insertion, so consider that rand may generates same outputs sometimes, why splay tree works much faster when it takes 1000000 distinct inputs? That doesn't make any sense to me. (I am a student and yea my English is bad sorry about it).

EDIT:

I modified vers.2 to:

    int x;
    double t1 = clock();
    for(int i = 0; i < 1000000; i++)
    {
        x = rand()%max_x;
        printf("%d",x);
        root = insert(root, i);
    }

    double t2=clock();
    double result = (t2-t1)/CLOCKS_PER_SEC;
    printf("insertion time: %f\n", result);

modified vers.2 output:

 ....a bunch of numbers...time: 0.569000

still faster than vers. 1

273K
  • 29,503
  • 10
  • 41
  • 64
  • In first case you are inserting random values, in the second only ones. That is not the same benchmark? – Quimby Jan 06 '23 at 09:55
  • sorry that is a typo, it is `i` but the result is similiar, it is still much faster than the first case – Kenny Ynnek Jan 06 '23 at 09:57
  • Calling `rand()` slows it down. You are comparing optimized builds I assume? – Ted Lyngmo Jan 06 '23 at 09:57
  • I modified it to```cpp for(int i = 0; i < 1000000; i++) { x = rand()%max_x; root = insert(root, i); }``` still much faster than the first case – Kenny Ynnek Jan 06 '23 at 09:58
  • 1
    Since you are not using `x` the compiler will most probably just throw that call to `rand` away from vers.2. – Ted Lyngmo Jan 06 '23 at 09:59
  • it makes sense, lemme modify it again – Kenny Ynnek Jan 06 '23 at 10:00
  • You are building a different tree, of course it will take different amount time? Insertion depth is crucial for running time. – Quimby Jan 06 '23 at 10:00
  • may you elaborate a lit bit more please? I think the depth of splaytree in vers.2 is deeper than that of vers.1 ? But why'd it be faster. Am I misunderstanding something? – Kenny Ynnek Jan 06 '23 at 10:06
  • When you insert items in order, you build a perfectly left-skewed tree, so inserts always find the nearest element at the root. You are basically pushing onto a linked list. That's about as cheap as insert can be. If you want to see this, try the animation at https://www.cs.usfca.edu/~galles/visualization/SplayTree.html If you look up some random elements between inserts, the tree will become more balanced so inserts will slow down. – Gene Jan 07 '23 at 03:36
  • thank you so much I get it now!!! How do I mark this as answered lol Im new to stackoverflow – Kenny Ynnek Jan 08 '23 at 13:16

0 Answers0