Hello,<br>
<br>
I'd first of all replace<br>
<span id="obmessage"><pre>mpz_init_set_ui (exp, 1);<br><br>by<br><span id="obmessage"><pre>mpz_set_ui (exp, 1);</pre></span>and redo the timing test (you already mpz_init-ed exp - and you will only mpz_clear it once)<br></pre></span>CU, JDO<br>
<div><br>
<blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;">----- Original Message -----<br>
From: "Nustenil Segundo" <nustenilsegundo@gmail.com><br>
To: gmp-discuss@swox.com<br>
Subject: GMP compared to JAVA<br>
Date: Thu, 20 Dec 2007 20:37:54 -0300<br>
<br>
<br>
Hi,<br>
<br>
I did a aplication with GMP and other same in JAVA.<br>
This aplication calculates the value of the operation a^b mod m, for<br>
a, b and m randomical numbers of n bits, n in interval [5, 25000).<br>
<br>
The code in C (using GMP):<br>
<br>
#include <gmp.h><br>
#include <stdio.h><br>
#include <stdlib.h><br>
#include <time.h><br>
<br>
<br>
int main()<br>
{<br>
//Declaração de variáveis<br>
mpz_t op1, op2, mod, x, aux, exp;<br>
gmp_randstate_t state;<br>
unsigned long int n = 4, seed, i, j;<br>
clock_t ini, fim, temp;<br>
FILE *p;<br>
<br>
srand(time(NULL));<br>
<br>
//inicialização das variáveis do tipo GMP<br>
mpz_init(op2);<br>
mpz_init(x);<br>
mpz_init(op1);<br>
mpz_init(mod);<br>
mpz_init(aux);<br>
mpz_init(exp);<br>
gmp_randinit_default(state);<br>
//abre o arquivo "arquivo.txt" para escrita<br>
p = fopen("arquivomassa.txt", "w");<br>
<br>
//laço para medir os bits e o tempo<br>
for(i=5 ; i<25000; i+=10){<br>
<br>
mpz_init_set_ui (exp, 1);<br>
seed= rand();<br>
gmp_randseed_ui (state,seed);<br>
//laço para o cálculo de 2^exp<br>
for(j=1; j<=i; j++){<br>
mpz_mul_ui(exp, exp, 2);<br>
}<br>
<br>
mpz_urandomb(aux, state, i); //aux Ã(c) um número<br>
aleatório de 0 a 2^i-1<br>
mpz_add (op1, aux, exp); //op1 Ã(c) um número de <br>
(i+1) bits<br>
<br>
mpz_urandomb(aux, state, i); //aux Ã(c) um número<br>
aleatório de 0 a 2^i-1<br>
mpz_add (op2, aux, exp); //op2 Ã(c) um número de <br>
(i+1) bits<br>
<br>
mpz_urandomb(aux, state, i); //aux Ã(c) um número<br>
aleatório de 0 a 2^i-1<br>
mpz_add (mod, aux, exp); //mod Ã(c) um número de <br>
(i+1) bits<br>
<br>
//calcula o tempo de [((op1)^(p2)) mod (mod)] e armazena em temp<br>
ini = clock();<br>
mpz_powm (x, op1, op2, mod);<br>
fim = clock();<br>
temp = fim - ini;<br>
<br>
printf("%lu\n", 1000*(unsigned long) temp/CLOCKS_PER_SEC);<br>
//armazena em arquivo.txt a quantidade de bits e o tempo<br>
da operação<br>
fprintf(p, "%lu\t%lu\n", i+1, 1000*(unsigned<br>
long)temp/CLOCKS_PER_SEC);<br>
}<br>
<br>
gmp_randclear (state);<br>
mpz_clear (op1);<br>
mpz_clear (op2);<br>
mpz_clear (mod);<br>
mpz_clear (x);<br>
mpz_clear (aux);<br>
mpz_clear (exp);<br>
<br>
<br>
return 0;<br>
}<br>
<br>
The code in JAVA:<br>
<br>
import java.io.IOException ;<br>
import java.io.RandomAccessFile;<br>
import java.math.BigDecimal;<br>
import java.math.BigInteger;<br>
import java.util.Calendar;<br>
<br>
<br>
<br>
public class Big {<br>
<br>
/**<br>
* @param args<br>
* @throws IOException<br>
*/<br>
public static void main(String[] args) throws IOException {<br>
// String saida = "";<br>
BigDecimal aux = new BigDecimal("16");<br>
// BigDecimal condicaoParada = new BigDecimal("10").pow(1000);<br>
long cont = 5;<br>
String fimDeLinha = System.getProperty(" line.separator");<br>
RandomAccessFile arquivo = new RandomAccessFile("tabela.txt","rw");<br>
while(cont < 25000){<br>
BigDecimal aleatorio = (new BigDecimal(<br>
Math.random()).multiply(aux)).add(aux);<br>
BigInteger p = new<br>
BigInteger(aleatorio.toString().substring(0, aleatorio.toString<br>
().indexOf(".")));<br>
aleatorio = (new <br>
BigDecimal(Math.random()).multiply(aux)).add(aux);<br>
BigInteger q = new<br>
BigInteger(aleatorio.toString().substring(0,<br>
aleatorio.toString().indexOf(".")));<br>
aleatorio = (new <br>
BigDecimal(Math.random()).multiply(aux)).add(aux);<br>
BigInteger m = new BigInteger(<br>
aleatorio.toString().substring(0, aleatorio.toString().indexOf(".")));<br>
// saida += ;<br>
String saida = cont+" \t"+calculaTempo(p, q, m)+fimDeLinha;<br>
arquivo.seek (arquivo.length());<br>
arquivo.write(saida.getBytes());<br>
System.out.print(saida);<br>
cont+=10;<br>
aux = aux.multiply(new BigDecimal("1024"));<br>
}<br>
// System.out.println(saida);<br>
}<br>
<br>
private static long calculaTempo(BigInteger p, BigInteger q, <br>
BigInteger m) {<br>
long tempoInicial = Calendar.getInstance().getTimeInMillis();<br>
BigInteger calculo = p.modPow(q, m);<br>
// BigInteger calculo = powBigInteger(p, q).mod(m);<br>
// q.<br>
long tempoFinal = Calendar.getInstance().getTimeInMillis();<br>
// System.out.println("------ "+calculo);<br>
return tempoFinal - tempoInicial;<br>
}<br>
<br>
<br>
}<br>
<br>
<br>
For the result in C with n = 10005 bits the time was 2800 ms (a example).<br>
<br>
For the result in JAVA with n = 10005 bits the time was 10 ms (a example).<br>
<br>
<br>
Why this difference?<br>
<br>
Thanks,<br>
<br>
Nustenil Segundo de Moraes Lima Marinus<br>
_______________________________________________<br>
gmp-discuss mailing list<br>
gmp-discuss@swox.com<br>
https://gmplib.org/mailman/listinfo/gmp-discuss<br>
</time.h></stdlib.h></stdio.h></gmp.h></nustenilsegundo@gmail.com></blockquote>
</div>
<BR>
--
<div> Got No Time? Shop Online for <a href=http://mail.shopping.com/?linkin_id=8033174 target="_blank"> <b> Great Gift Ideas!</b></a><br>
mail.com Shopping</div>