Gl Graph

Gl Graph Source of main-bench.cpp


 
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdint.h>
 
#include "Vec3f.h"
#include "Vec4f.h"
 
void Vec3AddOpe()
{
    Vec3f a=Vec3f(1,0,0);
    Vec3f b=Vec3f(0,1,0);
    Vec3f c=Vec3f(0,0,0);
    for (int i=0; i<50; i++)
    {
        c=c+a+b;
    }
    std::cout << c  << std::endl;
}
 
void Vec3AddFunc()
{
    Vec3f a=Vec3f(1,0,0);
    Vec3f b=Vec3f(0,1,0);
    Vec3f c=Vec3f(0,0,0);
    for (int i=0; i<500000000; i++)
    {
        c.add(c,a);
        c.add(c,b);
    }
    std::cout << c  << std::endl;
}
 
 
void Vec3Mult()
{
    float a=0.5;
    Vec3f b=Vec3f(0,1,0);
    Vec3f c=Vec3f(0,0,0);
    for (int i=0; i<500000000; i++)
    {
        Vec3f tmp=a*b;
        c=c+tmp;
    }
}
 
 
void Vec3Dot()
{
    Vec3f a=Vec3f(1,0,0);
    Vec3f b=Vec3f(0,1,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.dot(b);
    }
}
 
void Vec3Length()
{
    Vec3f a=Vec3f(1,0,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.length();
    }
}
 
void Vec3FastLength()
{
    Vec3f a=Vec3f(1,0,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.fastLength();
    }
}
 
void Vec4AddOpe()
{
    Vec4f a=Vec4f(1,0,0,0);
    Vec4f b=Vec4f(0,1,0,0);
    Vec4f c;
    for (int i=0; i<500000000; i++)
    {
        c=std::move(a+b);
    }
    std::cout << c  << std::endl;
}
 
void Vec4AddFunc()
{
    Vec4f a=Vec4f(1,0,0,0);
    Vec4f b=Vec4f(0,1,0,0);
    Vec4f c;
    for (int i=0; i<500000000; i++)
    {
        c.add(a,b);
    }
    std::cout << c  << std::endl;
}
 
void Vec4Mult()
{
    float a=0.5;
    Vec4f b=Vec4f(0,1,0,0);
    Vec4f c=Vec4f(0,0,0,0);
    for (int i=0; i<500000000; i++)
    {
        Vec4f tmp=a*b;
        c=c+tmp;
    }
    std::cout <<"Vec4Mult" << c  << std::endl;
}
 
 
void Vec4Dot()
{
    Vec4f a=Vec4f(1,0,0,0);
    Vec4f b=Vec4f(1,1,0,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.dot(b);
    }
 
}
 
void Vec4Length()
{
    Vec4f a=Vec4f(1,0,0,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.length();
    }
}
 
void Vec4FastLength()
{
    Vec4f a=Vec4f(1,0,0,0);
    float c=0;
    for (int i=0; i<500000000; i++)
    {
        c=c+a.fastLength();
    }
 
}
 
 
 
static double getDeltaTime(timespec &starting_time,timespec &endding_time)
{
    int64_t t1 = ((int64_t) starting_time.tv_sec)*1000000000 + starting_time.tv_nsec;
    int64_t t2 = ((int64_t) endding_time.tv_sec)*1000000000 + endding_time.tv_nsec;
    int64_t diff_time = t2 - t1;
    return diff_time/1.0e9;
}
 
 
typedef void (*PtrFonctBench)();
 
void bench(std::string benchname,PtrFonctBench func)
{
    timespec starting_time;
    timespec endding_time;
    clock_gettime(CLOCK_REALTIME, &starting_time);
    func();
    clock_gettime(CLOCK_REALTIME, &endding_time);
    std::cout << benchname << " : " << getDeltaTime(starting_time,endding_time) << std::endl;
}
 
 
 
 
class Vec4fTrace
{
private:
    float x;
    float y;
    float z;
    float w;
public:
 
    Vec4fTrace()
    {
    };
    Vec4fTrace(float x, float y, float z, float w)
    {
        this->x=x;
        this->y=y;
        this->z=z;
        this->w=w;
    };
 
    // c++ 11 move constructor
    Vec4fTrace( Vec4fTrace& arg)
    {
        std::cout << "Vec4fTrace constructor copy" << std::endl;
        this->x=arg.x;
        this->y=arg.y;
        this->z=arg.z;
        this->w=arg.w;
    }
    //c++ 11 move constructor
    Vec4fTrace( Vec4fTrace&& arg): x {arg.x},y {arg.y},z {arg.z},w {arg.w}
    {
        // trace here do not work
    }
 
    // c++ 11 move assignement
    Vec4fTrace& operator=(Vec4fTrace&& arg) {
        std::cout << "Vec4fTrace move assignement" << std::endl;
        this->x=arg.x;
        this->y=arg.y;
        this->z=arg.z;
        this->w=arg.w;
        return *this;
    };
 
 
    friend Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &b);
    friend Vec4fTrace operator+(Vec4fTrace &&a, Vec4fTrace &b);
    friend Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &&b);
    friend std::ostream& operator<<(std::ostream& stream, Vec4fTrace &m);
};
 
Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &b)
{
    std::cout << "Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &b)" << std::endl;
    Vec4fTrace tmp;
    tmp.x=a.x+b.x;
    tmp.y=a.y+b.y;
    tmp.z=a.z+b.z;
    return tmp;
}
Vec4fTrace operator+(Vec4fTrace &&a, Vec4fTrace &b)
{
    std::cout << "Vec4fTrace operator+(Vec4fTrace &&a, Vec4fTrace &b)" << std::endl;
    Vec4fTrace tmp;
    tmp.x=a.x+b.x;
    tmp.y=a.y+b.y;
    tmp.z=a.z+b.z;
    return tmp;
}
Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &&b)
{
    std::cout << "Vec4fTrace operator+(Vec4fTrace &a,  Vec4fTrace &&b)" << std::endl;
    Vec4fTrace tmp;
    tmp.x=a.x+b.x;
    tmp.y=a.y+b.y;
    tmp.z=a.z+b.z;
    return tmp;
}
 
std::ostream& operator<<(std::ostream& stream, Vec4fTrace &m)
{
    stream << "Vec4fTrace :" << m.x << " " << m.y << " " << m.z << " " << m.w;
    return stream;
}
 
void test_cpp11_A()
{
    std::cout << "start test c++ 11 A" << std::endl;
    Vec4fTrace a=Vec4fTrace(1,0,0,0);   // move constructor
    Vec4fTrace b=Vec4fTrace(0,1,0,0);   // move constructor
    Vec4fTrace c=Vec4fTrace(0,0,1,0);   // move constructor
    Vec4fTrace d(a+b);                  // move constructor
    std::cout << "loop" << std::endl;
    for (int i=0; i<3; i++)
    {
        c=a+b+c;// move assignement
    }
    std::cout << d << std::endl;
    std::cout << c << std::endl;
    std::cout << "end test c++ 11 A" << std::endl;
}
 
void test_cpp11_B()
{
    std::cout << "start test c++ 11 B" << std::endl;
    Vec4f a=Vec4f(1,0,0,0);   // move constructor
    Vec4f b=Vec4f(0,1,0,0);   // move constructor
    Vec4f c=Vec4f(0,0,1,0);   // move constructor
    Vec4f d(a+b);             // move constructor
    std::cout << "loop" << std::endl;
    for (int i=0; i<3; i++)
    {
        c=a+b+c;// move assignement
    }
    std::cout << d << std::endl;
    std::cout << c << std::endl;
    std::cout << "end test c++ 11 B" << std::endl;
}
 
 
 
int main(int argc, char *argv[])
{
 
    test_cpp11_A();
    test_cpp11_B();
 
    bench("Vec3f Add        ",Vec3AddOpe);
    bench("Vec3f Add        ",Vec3AddFunc);
    bench("Vec3f Mult       ",Vec3Mult);
    bench("Vec3f Dot        ",Vec3Dot);
    bench("Vec3f Lenght     ",Vec3Length);
    bench("Vec3f FastLength ",Vec3FastLength);
 
    bench("Vec4f Add        ",Vec4AddOpe);
    bench("Vec4f Add        ",Vec4AddFunc);
    bench("Vec4f Mult       ",Vec4Mult);
    bench("Vec4f Dot        ",Vec4Dot);
    bench("Vec4f Lenght     ",Vec4Length);
    bench("Vec4f FastLength ",Vec4FastLength);
    return 0;
}