⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ciperbench.cpp

📁 关于大数运算的加 减 乘 除 模 等运算 运用了libtommath库的算法
💻 CPP
字号:
// CiperBench.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "conio.h"
#include "CiperLib.h"
#include "clrcon.h"

#include <math.h>
#include <big.h>
#include <zzn.h>
#include <mmsystem.h>
#include <iostream>

using namespace std;
using namespace consolehelper;

 Miracl precision(5120);

void genBigInt(char *buf,long size){
    buf[0] = '1' + rand() * 8 / RAND_MAX;
    for (int i=1;i<size-1;i++)
    {
        buf[i] = '0' + rand() * 9 / RAND_MAX;
    }
    buf[size-1] = 0;
}

void Bench1()
{
    clr_restore();
    con_cls();
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("Mul Operation on Big Integers \n");
    clr_restore();
    clr_highlight(FALSE);

    printf("Running Miracl...please wait...\n");    
    char strData[10];
    DWORD using_time[100];

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Big bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<1000;i++)
        {
            
            bigSrcA *=bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_highlight();
    printf("Calc A=A*B with A,B assigning 10-bit dec number at the begining..\n");
    printf("Loop for 100*1000 times, the avg time for Miracl is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    clr_highlight(FALSE);
    printf("Running CiperLib...please wait...\n");

    

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Integer bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<1000;i++)
        {
            
            bigSrcA *= bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_restore();
    printf("Loop for 100*1000 times, the avg time for CiperLib is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();


    printf("finished\n");
    clr_restore();

}

void Bench2()
{
    clr_restore();
    con_cls();
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("Add Operation on Big Integers \n");
    clr_restore();
    clr_highlight(FALSE);

    printf("Running Miracl...please wait...\n");    
    char strData[10];
    DWORD using_time[100];

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Big bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA +=bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_highlight();
    printf("Calc A=A+B with A,B assigning 10-bit dec number at the begining..\n");
    printf("Loop for 100*1000 times, the avg time for Miracl is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    clr_highlight(FALSE);
    printf("Running CiperLib...please wait...\n");

    

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Integer bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA += bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_restore();
    printf("Loop for 100*1000 times, the avg time for CiperLib is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    printf("finished\n");
    clr_restore();

}


void Bench3()
{
    clr_restore();
    con_cls();
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("Sub Operation on Big Integers \n");
    clr_restore();
    clr_highlight(FALSE);

    printf("Running Miracl...please wait...\n");    
    char strData[10];
    DWORD using_time[100];

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Big bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA -=bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_highlight();
    printf("Calc A=A-B with A,B assigning 10-bit dec number at the begining..\n");
    printf("Loop for 100*1000 times, the avg time for Miracl is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    clr_highlight(FALSE);
    printf("Running CiperLib...please wait...\n");

    

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Integer bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA -= bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_restore();
    printf("Loop for 100*1000 times, the avg time for CiperLib is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    printf("finished\n");
    clr_restore();

}


void Bench4()
{
    clr_restore();
    con_cls();
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("Div Operation on Big Integers \n");
    clr_restore();
    clr_highlight(FALSE);

    printf("Running Miracl...please wait...\n");    
    char strData[10];
    DWORD using_time[100];

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Big bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA /=bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_highlight();
    printf("Calc A=A/B with A,B assigning 10-bit dec number at the begining..\n");
    printf("Loop for 100*1000 times, the avg time for Miracl is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    clr_highlight(FALSE);
    printf("Running CiperLib...please wait...\n");

    

    for (int j=0;j<100;j++)
    {
        using_time[j] = timeGetTime();
        Integer bigSrcA,bigSrcB;
        genBigInt(strData,sizeof(strData));
        bigSrcA = strData;
        genBigInt(strData,sizeof(strData));
        bigSrcB = strData;
        for(int i=0;i<10000;i++)
        {
            
            bigSrcA /= bigSrcB;
        }
        using_time[j] = timeGetTime()-using_time[j];
    }
    clr_restore();
    printf("Loop for 100*1000 times, the avg time for CiperLib is:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    for (int i=1;i<100;i++)
    {
        using_time[0] = (((__int64)using_time[0])*i + using_time[i])/(i+1);
    }
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    printf("finished\n");
    clr_restore();

}


void Bench5()
{

    clr_restore();
    con_cls();
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("Mod Operation on Zm \n");
    clr_restore();
    clr_highlight(FALSE);

    printf("Running Miracl...please wait...\n");    
    char strData[200];
    DWORD using_time[100];

  
    
    big src;
    src = mirvar(0);
    
   
    cinstr(src,"342424322354353263224223634534435237373454353463464354432");
    using_time[0] = timeGetTime();
    nxprime(src,src);
    using_time[0] = timeGetTime()-using_time[0];
   
    cotstr(src,strData);
    
    clr_highlight();
    
    printf("Finding a prime greater than num:342424322354353263224223634534435237373454353463464354432\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    printf("this prime is %s\n\n",strData);
    
    clr_restore();
    printf("Miracl used:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
    
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    clr_highlight(FALSE);
    printf("Running CiperLib...please wait...\n");
    Integer isrc = "342424322354353263224223634534435237373454353463464354432";
    ZModn::genPrime(isrc,isrc);//pre calc for speed up

    using_time[0] = timeGetTime();
    ZModn::genPrime(isrc,isrc);
    using_time[0] = timeGetTime()-using_time[0];
    isrc.toString(strData,200);
    
    clr_restore();
    printf("CiperLib used:\n");
    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
    clr_reverse();
  
    printf("%d ms\n\n",using_time[0]);
    clr_restore();
    printf("finished\n");
    clr_restore();

}
int _tmain(int argc, _TCHAR* argv[])
{
    Integer::Init(40960);
    DWORD seed;
    _asm {
        RDTSC
        mov seed,eax
    }
    srand(seed);

    clr_foregnd(CON_CLR::GREEN | CON_CLR::RED);
  
    printf("CiperLib Benchmark Program\n");
    clr_restore();

    printf("By CSK\n======================================\n\n");
    
    clr_foregnd( CON_CLR::RED);
    printf("CiperLib VS Miracl\n");
    clr_restore();

    printf("Press any key to proceed ");
    clr_reverse();
    printf("BenchMark#1");
    _getch();
    
    Bench1();

    printf("Press any key to proceed ");
    clr_reverse();
    printf("BenchMark#2");
    _getch();
    
    Bench2();

    printf("Press any key to proceed ");
    clr_reverse();
    printf("BenchMark#3");
    _getch();
    
    Bench3();

    printf("Press any key to proceed ");
    clr_reverse();
    printf("BenchMark#4");
    _getch();
    
    Bench4();

    printf("Press any key to proceed ");
    clr_reverse();
    printf("BenchMark#5");
    _getch();
    
    Bench5();
  
    clr_reverse();
    printf("BenchMark Finished");
    _getch();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -