SHARE
TWEET

app.js

a guest May 19th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {NativeModules, Platform, StyleSheet, Text, View, Button, TextInput} from 'react-native';
  3.  
  4. const MathFunc = require('NativeModules').MathFunc;
  5.  
  6. type State = { message: string };
  7.  
  8. export default class App extends Component {
  9.     constructor(props) {
  10.         super(props);
  11.         this.buttonCpp = this.buttonCpp.bind(this);
  12.         this.buttonBenchmarks = this.buttonBenchmarks.bind(this);
  13.         this.state = { message: "hd" };
  14.     }
  15.     async buttonCpp() {
  16.         var message = ""
  17.         var numberA = 3
  18.         var numberB = 2
  19.        
  20.         addResult = await MathFunc.add(numberA,numberB)
  21.         subtractResult = await MathFunc.add(numberA,numberB)
  22.         multiplyResult = await MathFunc.add(numberA,numberB)
  23.         divideResult = await MathFunc.add(numberA,numberB)
  24.        
  25.         var message = ""
  26.         message += numberA + "+" + numberB + "=" + addResult + "\n"
  27.         message += numberA + "-" + numberB + "=" + subtractResult + "\n"
  28.         message += numberA + "*" + numberB + "=" + multiplyResult + "\n"
  29.         message += numberA + "/" + numberB + "=" + divideResult + "\n"
  30.         this.setState({
  31.             message
  32.         });
  33.     }
  34.     n: number = 1000;
  35.     buttonBenchmarks(): void {
  36.    
  37.     var message = "Running benchmarks";
  38.     this.setState({
  39.         message
  40.     });
  41.     let dict_t = 0;
  42.     let patmch_1t = 0;
  43.     let patmch_2t = 0;
  44.     let matmul = 0;
  45.  
  46.     for (let i = 0; i < 10; ++i) {
  47.       matmul += this.testmatmul();
  48.     }
  49.     var message = "matmul: " + (matmul / 10 / 1000).toFixed(1) + "s\n";
  50.  
  51.     Array.from(Array(10).keys()).reduce((sequence, i) => {
  52.       return sequence.then(() => {
  53.         return this.testpatmch1();
  54.       }).then(t => patmch_1t += t);
  55.     }, Promise.resolve(0)).then(() => {
  56.       message += "patmch:1t: " + (patmch_1t / 10 / 1000).toFixed(1) + "s\n";
  57.     }).then(() => {
  58.       this.setState({
  59.         message
  60.     });
  61.     });
  62.   }
  63.   async testpatmch1(): Promise<number> {
  64.     let t0 = Date.now();
  65.     let t1 = Date.now();
  66.    
  67.     return Date.now();
  68.   }
  69.   testmatmul(): number {
  70.     let t0 = Date.now();
  71. //    let a = this.matgen(this.n);
  72. //    let b = this.matgen(this.n);
  73. //    let c = this.multiply(a, b);
  74.     let t1 = Date.now();
  75.     return Date.now();
  76.   }
  77.   transpose(a: any): any {
  78.     let b = [], m = a.length, n = a[0].length; // m rows and n cols
  79.       for (let j = 0; j < n; ++j) b[j] = [];
  80.       for (let i = 0; i < m; ++i)
  81.           for (let j = 0; j < n; ++j)
  82.               b[j].push(a[i][j]);
  83.       return b;
  84.   }
  85.   multiply(a: any, b: any): any {
  86.     let m = a.length, n = a[0].length, s = b.length, t = b[0].length;
  87.       if (n != s) return null;
  88.       let x = [], c = this.transpose(b);
  89.       for (let i = 0; i < m; ++i) {
  90.           x[i] = [];
  91.           for (let j = 0; j < t; ++j) {
  92.               let sum = 0;
  93.               let ai = a[i], cj = c[j];
  94.               for (let k = 0; k < n; ++k) sum += ai[k] * cj[k];
  95.               x[i].push(sum);
  96.           }
  97.       }
  98.       return x;
  99.   }
  100.   matgen(n: number): any {
  101.       let a = [], tmp = 1. / n / n;
  102.       for (let i = 0; i < n; ++i) {
  103.           a[i] = []
  104.           for (let j = 0; j < n; ++j)
  105.               a[i][j] = tmp * (i - j) * (i + j);
  106.       }
  107.       return a;
  108.   }
  109.     render() {
  110.         return (
  111.         <View style={styles.container}>
  112.             <Text>
  113.                 Welcome to React Native!
  114.             </Text>
  115.             <View style={{margin: 20}}>
  116.                 <Button
  117.                     style={{margin: 100}}
  118.                     onPress={this.buttonCpp}
  119.                     title="Test C++"
  120.                 />
  121.             </View>
  122.             <View style={{margin: 20}}>
  123.                 <Button
  124.                     style={{padding: 100}}
  125.                     onPress={this.buttonBenchmarks}
  126.                     title="Run benchmarks"
  127.                 />
  128.             </View>
  129.             <View style={{padding: 10}}>
  130.                 <TextInput
  131.                     style={{height: 100}}
  132.                     placeholder="Type here to translate!"
  133.                     multiline={true}
  134.                     value={this.state.message}
  135.                 />
  136.             </View>
  137.       </View>
  138.     );
  139.   }
  140. }
  141.  
  142. const styles = StyleSheet.create({
  143.   container: {
  144.     flex: 1,
  145.     justifyContent: 'center',
  146.     alignItems: 'center',
  147.   },
  148. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top