Coder Social home page Coder Social logo

c-vs-tensorflow's People

Contributors

parmusingh avatar

Stargazers

 avatar

Watchers

 avatar

c-vs-tensorflow's Issues

Some Comparisons on Improved Code

I tried to improve code for TensorFlow (~17mins) and saw that this code improved performance for me:

import numpy as np
import time
import os

inputs = [331, 884, 254,..]

with tf.Graph().as_default():
    inputs = tf.Variable(inputs, dtype=np.float32)
    encrypt = (tf.sqrt(inputs) + 523) / 5.3

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        print('Starting')
        t1 = time.time()
        for I in range(1000000):
            encrypted_data = []
            encrypted_data.extend(encrypt.eval())
            print(I)
        print('completed in : {}s'.format(time.time() - t1))

Torch gave me even better performance though (~15mins):

import time
import os


inputs = [331, 884, 254,...]
inputs = torch.tensor(inputs, dtype=torch.float32)
in_ = inputs.to('cuda:0') # comment this if you don't have GPU support

encrypt = lambda x: (torch.sqrt(x) + 523) / 5.3

print('Starting')
t1 = time.time()

for I in range(1000000):
    encrypted_data = []
    encrypted_data.extend(encrypt(in_).tolist())
    print(I, " ")

print('completed in : {}s'.format(time.time() - t1))

C++ code gave me (~24mins)

After doing this though, I thought I gave advantage of vectorization to TensorFlow and PyTorch. So I, updated C++ code:

#include<math.h>
#include<time.h>
using namespace std;

static inline float encrypt (float x) { return (sqrt(x) + 523) / 5.3; }

int main(){
    vector<float> inputs {331, 884, 254,...};
    clock_t start;
    
    cout<<"Starting\n";
    start = clock();
    
    for(int I = 0; I<1000000; I++){
        vector<float> encryptedData(10000);
	transform(inputs.begin(), inputs.end(), encryptedData.begin(), encrypt);
	cout<<I<<endl;
    }
	
    cout<<"Time "<<((float)clock() - (float)start)/CLOCKS_PER_SEC;
    return 0;
}

This code gave even tough competition (~14mins) to TF and Torch.

Conclusion

Code optimization is much important to work on as we can never know of all the functions/algorithms/methods in a certain framework or language that can help us improve the performance 10 folds.

Configuration

CPU: i5 5th gen
GPU: Nvidia GTX 940m 2GB

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.