Instantly share code, notes, and snippets.

Embed
What would you like to do?
CVM Performance Test: Multiplication
mulloops.mul(1000000)
var mulloopsABI = [{"constant":true,"inputs":[{"name":"x","type":"uint256"}],"name":"mul","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}];
var mulloopsBC = '0x608060405234801561001057600080fd5b506101f0806100206000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063131e2f1814610046575b600080fd5b34801561005257600080fd5b50610071600480360381019080803590602001909291905050506100ec565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b1578082015181840152602081019050610096565b50505050905090810190601f1680156100de5780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b60608060405160206040519081016040526001905260206040519081016040527f0900000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528460206040519081016040525280604051819003602090039052907f6d756c0000000000000000000000000000000000000000000000000000000000f59050809150509190505600a165627a7a7230582009d118142d26f987c1bb8cc0acdaeb4dea8e5b2d2c4c3d8cb4daa5c6bceedc7b0029';
var mulloopsContract = web3.cmt.contract(mulloopsABI);
var mulloops = mulloopsContract.new(
{
from: web3.cmt.accounts[0],
data: mulloopsBC,
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
#include <eni.h>
#include <string>
class Multiplication : public eni::EniBase
{
public:
Multiplication(const std::string& pArgStr) : eni::EniBase(pArgStr) { }
~Multiplication() { }
private:
bool parse(const json::Array& pArgs) override {
x = pArgs[0].toInteger();
return true;
}
eni::Gas gas() const override {
return 10000;
}
bool run(json::Array& pRetVal) override {
eni::Int sum = 0;
eni::Int prod = 2;
for(eni::Int i=0;i<x;i++){
prod = prod * (i%10 + 1);
if (prod > 1000) {
prod = 2;
sum = sum + 1;
}
}
std::string s=boost::lexical_cast<std::string>(sum);
pRetVal.emplace_back(s);
return true;
}
private:
eni::Int x;
};
ENI_C_INTERFACE(mul, Multiplication)
CPPFLAGS=-I/home/ubuntu/libeni/include
CXXFLAGS=-std=c++11 -fPIC
LDFLAGS=-L/home/ubuntu/libeni/lib
LDADD=-leni
all:
g++ ${CPPFLAGS} ${CXXFLAGS} ${LDFLAGS} -shared -oeni_multiplication.so eni_multiplication.cpp ${LDADD}
pragma solidity ^0.4.23;
contract mulloops {
function mul(uint256 x) public pure returns(string) {
string memory sum;
sum=eni("mul", x);
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment