Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Coin-Toss Simulator Lagging
    primarykey
    data
    text
    <p>Ok I am new to android programming but i have created an app as follows.</p> <pre><code>package com.michaelpeerman.probability; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.Random; public class ProbabilityActivity extends Activity { private EditText number; private Button submit; public void flipcoin(int numberint) { TextView result = (TextView)findViewById(R.id.result); int heads = 0; int tails = 0; for (int j = 0; j &lt; numberint; j++) { int i = 1 + new Random().nextInt(2); if (i == 1) heads++; if (i == 2) tails++; result.setText("heads : " + heads + "\ntails : " + tails); } } public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); setContentView(R.layout.main); submit = ((Button)findViewById(R.id.submit)); submit.setOnClickListener(new View.OnClickListener() { public void onClick(View paramView) { number = ((EditText)findViewById(R.id.number)); int numberint = Integer.parseInt(number.getText().toString()); flipcoin(numberint); } }); } } </code></pre> <p>But this runs incredibly slow compared to</p> <pre><code>package com.michaelpeerman.probability; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.Random; public class ProbabilityActivity extends Activity { public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); setContentView(R.layout.main); TextView result = (TextView)findViewById(R.id.result); int heads = 0; int tails = 0; for (int j = 0; j &lt; 50000; j++) { int i = 1 + new Random().nextInt(2); if (i == 1) heads++; if (i == 2) tails++; result.setText("heads : " + heads + "\ntails : " + tails); } } </code></pre> <p>I do not understand why this is. They are both using the same code except the first one lets you enter in a number and press submit before running the loop. Shouldnt they be the same speed? What can I do to speed it up?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload