This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Diagnostics; | |
| namespace CSharpBasic | |
| { | |
| class Program | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static string reverseByAPI(string input){ | |
| if (string.IsNullOrEmpty(input)) return input; | |
| char[] inputArrary = input.ToCharArray(); | |
| Array.Reverse(inputArrary); | |
| return new string(inputArrary); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static string reverseByArray(string input) | |
| { | |
| if (string.IsNullOrEmpty(input)) return input; | |
| int halfLength = input.Length >> 1; | |
| char[] inputArray = input.ToCharArray(); | |
| char temp; | |
| for (int i = 0; i < halfLength; i++) | |
| { | |
| temp = inputArray[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 3611 ticks. ReverseByAPI(Length:1), runs 10000 times. | |
| 3581 ticks. ReverseByXor(Length:1), runs 10000 times. | |
| =============== | |
| 4955 ticks. ReverseByAPI(Length:10), runs 10000 times. | |
| 5349 ticks. ReverseByXor(Length:10), runs 10000 times. | |
| =============== | |
| 9553 ticks. ReverseByAPI(Length:100), runs 10000 times. | |
| 27669 ticks. ReverseByXor(Length:100), runs 10000 times. | |
| =============== | |
| 120702 ticks. ReverseByAPI(Length:1000), runs 10000 times. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package basic.java; | |
| interface Reverse{ | |
| public String reverse(String s); | |
| } | |
| class ReverseByArray implements Reverse{ | |
| public String reverse(String s){ | |
| if (null == s || s.equals("")) return s; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original String:1234567890 | |
| Reversed String: 0987654321(Reverse by array) | |
| Original String:1234567890 | |
| Reversed String: 0987654321(Reverse by array 2) | |
| Original String:1234567890 | |
| Reversed String: 0987654321(Reverse by api) | |
| -------- | |
| Original String:123 | |
| Reversed String: 321(Reverse by array) | |
| Original String:123 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public final class Singleton { | |
| private static final Singleton INSTANCE = new Singleton(); | |
| private Singleton(){ | |
| // Do nothing, make sure user can not create object directly | |
| if (null != INSTANCE){ | |
| throw new IllegalArgumentException("Already instantiated."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public final class Singleton3{ | |
| private static class Handler{ | |
| private static final Singleton3 INSTANCE = new Singleton3(); | |
| } | |
| private Singleton3(){ | |
| if (null != Handler.INSTANCE){ | |
| throw new IllegalArgumentException("Already instantiated."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Singleton5 { | |
| private static volatile Singleton5 INSTANCE = null; | |
| private Singleton5(){ | |
| if (null != INSTANCE){ | |
| throw new IllegalArgumentException("Already instantiated."); | |
| } | |
| } | |
| public static Singleton5 getInstance(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum Singleton6{ | |
| INSTANCE; | |
| private Object s = new Object(); | |
| // Invoke this like | |
| // Singleton6.INSTANCE.doSomething(); | |
| public Object doSomething(){ | |
| return s; | |
| } |
OlderNewer